Skip to content

Commit 6a18bd4

Browse files
committed
feat: add portal protocol option to yalc add
1 parent 3b834e4 commit 6a18bd4

File tree

5 files changed

+108
-9
lines changed

5 files changed

+108
-9
lines changed

src/add.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface AddPackagesOptions {
2424
dev?: boolean
2525
link?: boolean
2626
linkDep?: boolean
27+
portalDep?: boolean
2728
replace?: boolean
2829
update?: boolean
2930
safe?: boolean
@@ -58,6 +59,18 @@ const checkPnpmWorkspace = (workingDir: string) => {
5859
return fs.existsSync(join(workingDir, 'pnpm-workspace.yaml'))
5960
}
6061

62+
const getProtocol = (options: { linkDep?: boolean, portalDep?: boolean }) => {
63+
if (options.portalDep) {
64+
return 'portal:';
65+
}
66+
67+
if (options.linkDep) {
68+
return 'link:';
69+
}
70+
71+
return 'file:';
72+
}
73+
6174
export const addPackages = async (
6275
packages: string[],
6376
options: AddPackagesOptions
@@ -84,6 +97,7 @@ export const addPackages = async (
8497

8598
let pnpmWorkspace = false
8699

100+
const isLinkLike = options.link || options.linkDep || options.portalDep;
87101
const doPure =
88102
options.pure === false
89103
? false
@@ -162,20 +176,21 @@ export const addPackages = async (
162176
)} purely`
163177
)
164178
}
179+
165180
if (!doPure) {
166181
const destModulesDir = join(workingDir, 'node_modules', name)
167-
if (options.link || options.linkDep || isSymlink(destModulesDir)) {
182+
if (isLinkLike || isSymlink(destModulesDir)) {
168183
fs.removeSync(destModulesDir)
169184
}
170185

171-
if (options.link || options.linkDep) {
186+
if (isLinkLike) {
172187
ensureSymlinkSync(destYalcCopyDir, destModulesDir, 'junction')
173188
} else {
174189
await copyDirSafe(destYalcCopyDir, destModulesDir, !options.replace)
175190
}
176191

177192
if (!options.link) {
178-
const protocol = options.linkDep ? 'link:' : 'file:'
193+
const protocol = getProtocol(options);
179194
const localAddress = options.workspace
180195
? 'workspace:*'
181196
: protocol + values.yalcPackagesFolder + '/' + pkg.name
@@ -211,7 +226,7 @@ export const addPackages = async (
211226
replacedVersion = replacedVersion == localAddress ? '' : replacedVersion
212227
}
213228

214-
if (pkg.bin && (options.link || options.linkDep)) {
229+
if (pkg.bin && isLinkLike) {
215230
const binDir = join(workingDir, 'node_modules', '.bin')
216231
const addBinScript = (src: string, dest: string) => {
217232
const srcPath = join(destYalcCopyDir, src)
@@ -274,8 +289,9 @@ export const addPackages = async (
274289
workspace: options.workspace,
275290
file: options.workspace
276291
? undefined
277-
: !options.link && !options.linkDep && !doPure,
278-
link: options.linkDep && !doPure,
292+
: !isLinkLike && !doPure,
293+
link: !options.portalDep && options.linkDep && !doPure,
294+
portal: options.portalDep && !doPure,
279295
signature: i.signature,
280296
})),
281297
{ workingDir: options.workingDir }

src/lockfile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type LockFilePackageEntry = {
1414
version?: string
1515
file?: boolean
1616
link?: boolean
17+
portal?: boolean
1718
replaced?: string
1819
signature?: string
1920
pure?: boolean
@@ -90,7 +91,7 @@ export const addPackageToLockfile = (
9091
) => {
9192
const lockfile = readLockfile(options)
9293
packages.forEach(
93-
({ name, version, file, link, replaced, signature, pure, workspace }) => {
94+
({ name, version, file, link, portal, replaced, signature, pure, workspace }) => {
9495
let old = lockfile.packages[name] || {}
9596
lockfile.packages[name] = {}
9697
if (version) {
@@ -105,6 +106,9 @@ export const addPackageToLockfile = (
105106
if (link) {
106107
lockfile.packages[name].link = true
107108
}
109+
if (portal) {
110+
lockfile.packages[name].portal = true
111+
}
108112
if (pure) {
109113
lockfile.packages[name].pure = true
110114
}

src/update.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const updatePackages = async (
4848
file: lockfile.packages[name].file,
4949
link: lockfile.packages[name].link,
5050
pure: lockfile.packages[name].pure,
51+
portal: lockfile.packages[name].portal,
5152
workspace: lockfile.packages[name].workspace,
5253
}))
5354

@@ -68,7 +69,7 @@ export const updatePackages = async (
6869
})
6970

7071
const packagesLinks = lockPackages
71-
.filter((p) => !p.file && !p.link && !p.pure && !p.workspace)
72+
.filter((p) => !p.file && !p.link && !p.portal && !p.pure && !p.workspace)
7273
.map((p) => p.name)
7374
await addPackages(packagesLinks, {
7475
...addOpts,
@@ -83,6 +84,13 @@ export const updatePackages = async (
8384
pure: false,
8485
})
8586

87+
const packagesPortalDep = lockPackages.filter((p) => p.portal).map((p) => p.name)
88+
await addPackages(packagesPortalDep, {
89+
...addOpts,
90+
portalDep: true,
91+
pure: false,
92+
})
93+
8694
const packagesLinkDep = lockPackages.filter((p) => p.link).map((p) => p.name)
8795
await addPackages(packagesLinkDep, {
8896
...addOpts,

src/yalc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ yargs
154154
describe: 'Add package from yalc repo to the project',
155155
builder: () => {
156156
return yargs
157-
.boolean(['file', 'dev', 'link', ...updateFlags])
157+
.boolean(['file', 'dev', 'link', 'portal', ...updateFlags])
158158
.alias('D', 'dev')
159159
.boolean('workspace')
160160
.alias('save-dev', 'dev')
@@ -166,6 +166,7 @@ yargs
166166
return addPackages(argv._.slice(1), {
167167
dev: argv.dev,
168168
linkDep: argv.link,
169+
portalDep: argv.portal,
169170
restore: argv.restore,
170171
pure: argv.pure,
171172
workspace: argv.workspace,

test/index.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,74 @@ describe('Yalc package manager', function () {
514514
})
515515
})
516516
})
517+
518+
describe('Add package (--portal)', () => {
519+
before(() => {
520+
return addPackages([values.depPackage], {
521+
workingDir: projectDir,
522+
portalDep: true,
523+
})
524+
})
525+
it('copies package to .yalc folder', () => {
526+
checkExists(join(projectDir, '.yalc', values.depPackage))
527+
})
528+
it('copies remove package to node_modules', () => {
529+
checkExists(join(projectDir, 'node_modules', values.depPackage))
530+
})
531+
it('creates to yalc.lock', () => {
532+
checkExists(join(projectDir, 'yalc.lock'))
533+
})
534+
it('places yalc.lock correct info about file', () => {
535+
const lockFile = readLockfile({ workingDir: projectDir })
536+
deepEqual(lockFile.packages, {
537+
[values.depPackage]: {
538+
portal: true,
539+
replaced: 'link:.yalc/' + values.depPackage,
540+
signature: extractSignature(lockFile, values.depPackage),
541+
},
542+
})
543+
})
544+
it('updates package.json', () => {
545+
const pkg = readPackageManifest(projectDir)!
546+
deepEqual(pkg.dependencies, {
547+
[values.depPackage]: 'portal:.yalc/' + values.depPackage,
548+
})
549+
})
550+
it('create and updates installations file', () => {
551+
const installtions = readInstallationsFile()
552+
deepEqual(installtions, {
553+
[values.depPackage]: [projectDir],
554+
})
555+
})
556+
})
557+
558+
describe('Updated linked (--portal) package', () => {
559+
before(() => {
560+
return updatePackages([values.depPackage], {
561+
workingDir: projectDir,
562+
})
563+
})
564+
it('places yalc.lock correct info about file', () => {
565+
const lockFile = readLockfile({ workingDir: projectDir })
566+
deepEqual(lockFile.packages, {
567+
[values.depPackage]: {
568+
portal: true,
569+
replaced: 'link:.yalc/' + values.depPackage,
570+
signature: extractSignature(lockFile, values.depPackage),
571+
},
572+
})
573+
})
574+
it('updates package.json', () => {
575+
const pkg = readPackageManifest(projectDir)!
576+
deepEqual(pkg.dependencies, {
577+
[values.depPackage]: 'portal:.yalc/' + values.depPackage,
578+
})
579+
})
580+
it('create and updates installations file', () => {
581+
const installtions = readInstallationsFile()
582+
deepEqual(installtions, {
583+
[values.depPackage]: [projectDir],
584+
})
585+
})
586+
})
517587
})

0 commit comments

Comments
 (0)