Skip to content

Commit

Permalink
feat: add portal protocol option to yalc add
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobmarcos committed Jul 21, 2023
1 parent 3b834e4 commit 6a18bd4
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface AddPackagesOptions {
dev?: boolean
link?: boolean
linkDep?: boolean
portalDep?: boolean
replace?: boolean
update?: boolean
safe?: boolean
Expand Down Expand Up @@ -58,6 +59,18 @@ const checkPnpmWorkspace = (workingDir: string) => {
return fs.existsSync(join(workingDir, 'pnpm-workspace.yaml'))
}

const getProtocol = (options: { linkDep?: boolean, portalDep?: boolean }) => {
if (options.portalDep) {
return 'portal:';
}

if (options.linkDep) {
return 'link:';
}

return 'file:';
}

export const addPackages = async (
packages: string[],
options: AddPackagesOptions
Expand All @@ -84,6 +97,7 @@ export const addPackages = async (

let pnpmWorkspace = false

const isLinkLike = options.link || options.linkDep || options.portalDep;
const doPure =
options.pure === false
? false
Expand Down Expand Up @@ -162,20 +176,21 @@ export const addPackages = async (
)} purely`
)
}

if (!doPure) {
const destModulesDir = join(workingDir, 'node_modules', name)
if (options.link || options.linkDep || isSymlink(destModulesDir)) {
if (isLinkLike || isSymlink(destModulesDir)) {
fs.removeSync(destModulesDir)
}

if (options.link || options.linkDep) {
if (isLinkLike) {
ensureSymlinkSync(destYalcCopyDir, destModulesDir, 'junction')
} else {
await copyDirSafe(destYalcCopyDir, destModulesDir, !options.replace)
}

if (!options.link) {
const protocol = options.linkDep ? 'link:' : 'file:'
const protocol = getProtocol(options);
const localAddress = options.workspace
? 'workspace:*'
: protocol + values.yalcPackagesFolder + '/' + pkg.name
Expand Down Expand Up @@ -211,7 +226,7 @@ export const addPackages = async (
replacedVersion = replacedVersion == localAddress ? '' : replacedVersion
}

if (pkg.bin && (options.link || options.linkDep)) {
if (pkg.bin && isLinkLike) {
const binDir = join(workingDir, 'node_modules', '.bin')
const addBinScript = (src: string, dest: string) => {
const srcPath = join(destYalcCopyDir, src)
Expand Down Expand Up @@ -274,8 +289,9 @@ export const addPackages = async (
workspace: options.workspace,
file: options.workspace
? undefined
: !options.link && !options.linkDep && !doPure,
link: options.linkDep && !doPure,
: !isLinkLike && !doPure,
link: !options.portalDep && options.linkDep && !doPure,
portal: options.portalDep && !doPure,
signature: i.signature,
})),
{ workingDir: options.workingDir }
Expand Down
6 changes: 5 additions & 1 deletion src/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type LockFilePackageEntry = {
version?: string
file?: boolean
link?: boolean
portal?: boolean
replaced?: string
signature?: string
pure?: boolean
Expand Down Expand Up @@ -90,7 +91,7 @@ export const addPackageToLockfile = (
) => {
const lockfile = readLockfile(options)
packages.forEach(
({ name, version, file, link, replaced, signature, pure, workspace }) => {
({ name, version, file, link, portal, replaced, signature, pure, workspace }) => {
let old = lockfile.packages[name] || {}
lockfile.packages[name] = {}
if (version) {
Expand All @@ -105,6 +106,9 @@ export const addPackageToLockfile = (
if (link) {
lockfile.packages[name].link = true
}
if (portal) {
lockfile.packages[name].portal = true
}
if (pure) {
lockfile.packages[name].pure = true
}
Expand Down
10 changes: 9 additions & 1 deletion src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const updatePackages = async (
file: lockfile.packages[name].file,
link: lockfile.packages[name].link,
pure: lockfile.packages[name].pure,
portal: lockfile.packages[name].portal,
workspace: lockfile.packages[name].workspace,
}))

Expand All @@ -68,7 +69,7 @@ export const updatePackages = async (
})

const packagesLinks = lockPackages
.filter((p) => !p.file && !p.link && !p.pure && !p.workspace)
.filter((p) => !p.file && !p.link && !p.portal && !p.pure && !p.workspace)
.map((p) => p.name)
await addPackages(packagesLinks, {
...addOpts,
Expand All @@ -83,6 +84,13 @@ export const updatePackages = async (
pure: false,
})

const packagesPortalDep = lockPackages.filter((p) => p.portal).map((p) => p.name)
await addPackages(packagesPortalDep, {
...addOpts,
portalDep: true,
pure: false,
})

const packagesLinkDep = lockPackages.filter((p) => p.link).map((p) => p.name)
await addPackages(packagesLinkDep, {
...addOpts,
Expand Down
3 changes: 2 additions & 1 deletion src/yalc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ yargs
describe: 'Add package from yalc repo to the project',
builder: () => {
return yargs
.boolean(['file', 'dev', 'link', ...updateFlags])
.boolean(['file', 'dev', 'link', 'portal', ...updateFlags])
.alias('D', 'dev')
.boolean('workspace')
.alias('save-dev', 'dev')
Expand All @@ -166,6 +166,7 @@ yargs
return addPackages(argv._.slice(1), {
dev: argv.dev,
linkDep: argv.link,
portalDep: argv.portal,
restore: argv.restore,
pure: argv.pure,
workspace: argv.workspace,
Expand Down
70 changes: 70 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,74 @@ describe('Yalc package manager', function () {
})
})
})

describe('Add package (--portal)', () => {
before(() => {
return addPackages([values.depPackage], {
workingDir: projectDir,
portalDep: true,
})
})
it('copies package to .yalc folder', () => {
checkExists(join(projectDir, '.yalc', values.depPackage))
})
it('copies remove package to node_modules', () => {
checkExists(join(projectDir, 'node_modules', values.depPackage))
})
it('creates to yalc.lock', () => {
checkExists(join(projectDir, 'yalc.lock'))
})
it('places yalc.lock correct info about file', () => {
const lockFile = readLockfile({ workingDir: projectDir })
deepEqual(lockFile.packages, {
[values.depPackage]: {
portal: true,
replaced: 'link:.yalc/' + values.depPackage,
signature: extractSignature(lockFile, values.depPackage),
},
})
})
it('updates package.json', () => {
const pkg = readPackageManifest(projectDir)!
deepEqual(pkg.dependencies, {
[values.depPackage]: 'portal:.yalc/' + values.depPackage,
})
})
it('create and updates installations file', () => {
const installtions = readInstallationsFile()
deepEqual(installtions, {
[values.depPackage]: [projectDir],
})
})
})

describe('Updated linked (--portal) package', () => {
before(() => {
return updatePackages([values.depPackage], {
workingDir: projectDir,
})
})
it('places yalc.lock correct info about file', () => {
const lockFile = readLockfile({ workingDir: projectDir })
deepEqual(lockFile.packages, {
[values.depPackage]: {
portal: true,
replaced: 'link:.yalc/' + values.depPackage,
signature: extractSignature(lockFile, values.depPackage),
},
})
})
it('updates package.json', () => {
const pkg = readPackageManifest(projectDir)!
deepEqual(pkg.dependencies, {
[values.depPackage]: 'portal:.yalc/' + values.depPackage,
})
})
it('create and updates installations file', () => {
const installtions = readInstallationsFile()
deepEqual(installtions, {
[values.depPackage]: [projectDir],
})
})
})
})

0 comments on commit 6a18bd4

Please sign in to comment.