From d797b8d44b1d2571b9aee9dc47032615a45b9c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moreno?= Date: Tue, 2 Nov 2021 11:47:06 +0100 Subject: [PATCH] feat: add --no-dependencies flag closes #439 --- src/api.ts | 8 ++--- src/main.ts | 11 ++++-- src/npm.ts | 14 +++++--- src/package.ts | 77 +++++++++++++++++++++++++++------------- src/publish.ts | 1 + src/test/package.test.ts | 12 +++++++ 6 files changed, 86 insertions(+), 37 deletions(-) diff --git a/src/api.ts b/src/api.ts index 1aea5b50..99c21e51 100644 --- a/src/api.ts +++ b/src/api.ts @@ -83,6 +83,7 @@ export interface IPublishOptions { export enum PackageManager { Npm, Yarn, + None, } export interface IListFilesOptions { @@ -138,12 +139,7 @@ export function publish(options: IPublishOptions = {}): Promise { * Lists the files included in the extension's package. */ export function listFiles(options: IListFilesOptions = {}): Promise { - return _listFiles( - options.cwd, - options.packageManager === PackageManager.Yarn, - options.packagedDependencies, - options.ignoreFile - ); + return _listFiles(options); } /** diff --git a/src/main.ts b/src/main.ts index 842ecd5f..f72c71fd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -70,8 +70,9 @@ module.exports = function (argv: string[]): void { undefined ) .option('--ignoreFile ', 'Indicate alternative .vscodeignore') - .action(({ yarn, packagedDependencies, ignoreFile }) => - main(ls(undefined, yarn, packagedDependencies, ignoreFile)) + .option('--no-dependencies', 'Disable dependency detection via npm or yarn') + .action(({ yarn, packagedDependencies, ignoreFile, dependencies }) => + main(ls({ useYarn: yarn, packagedDependencies, ignoreFile, dependencies })) ); program @@ -96,6 +97,7 @@ module.exports = function (argv: string[]): void { .option('--ignoreFile ', 'Indicate alternative .vscodeignore') .option('--no-gitHubIssueLinking', 'Disable automatic expansion of GitHub-style issue syntax into links') .option('--no-gitLabIssueLinking', 'Disable automatic expansion of GitLab-style issue syntax into links') + .option('--no-dependencies', 'Disable dependency detection via npm or yarn') .action( ( version, @@ -112,6 +114,7 @@ module.exports = function (argv: string[]): void { ignoreFile, gitHubIssueLinking, gitLabIssueLinking, + dependencies, } ) => main( @@ -129,6 +132,7 @@ module.exports = function (argv: string[]): void { ignoreFile, gitHubIssueLinking, gitLabIssueLinking, + dependencies, }) ) ); @@ -159,6 +163,7 @@ module.exports = function (argv: string[]): void { .option('--no-yarn', 'Use npm instead of yarn (default inferred from lack of yarn.lock or .yarnrc)') .option('--noVerify') .option('--ignoreFile ', 'Indicate alternative .vscodeignore') + .option('--no-dependencies', 'Disable dependency detection via npm or yarn') .action( ( version, @@ -175,6 +180,7 @@ module.exports = function (argv: string[]): void { yarn, noVerify, ignoreFile, + dependencies, } ) => main( @@ -192,6 +198,7 @@ module.exports = function (argv: string[]): void { useYarn: yarn, noVerify, ignoreFile, + dependencies, }) ) ); diff --git a/src/npm.ts b/src/npm.ts index 4bb258f0..ae6541aa 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -201,7 +201,7 @@ async function getYarnDependencies(cwd: string, packagedDependencies?: string[]) return _.uniq(result); } -export async function detectYarn(cwd: string) { +export async function detectYarn(cwd: string): Promise { for (const name of ['yarn.lock', '.yarnrc', '.yarnrc.yaml', '.pnp.cjs', '.yarn']) { if (await exists(path.join(cwd, name))) { if (!process.env['VSCE_TESTS']) { @@ -217,12 +217,16 @@ export async function detectYarn(cwd: string) { export async function getDependencies( cwd: string, - useYarn?: boolean, + dependencies: 'npm' | 'yarn' | 'none' | undefined, packagedDependencies?: string[] ): Promise { - return (useYarn !== undefined ? useYarn : await detectYarn(cwd)) - ? await getYarnDependencies(cwd, packagedDependencies) - : await getNpmDependencies(cwd); + if (dependencies === 'none') { + return [cwd]; + } else if (dependencies === 'yarn' || (dependencies === undefined && (await detectYarn(cwd)))) { + return await getYarnDependencies(cwd, packagedDependencies); + } else { + return await getNpmDependencies(cwd); + } } export function getLatestVersion(name: string, cancellationToken?: CancellationToken): Promise { diff --git a/src/package.ts b/src/package.ts index b81403a0..119544b7 100644 --- a/src/package.ts +++ b/src/package.ts @@ -97,6 +97,7 @@ export interface IPackageOptions { readonly ignoreFile?: string; readonly gitHubIssueLinking?: boolean; readonly gitLabIssueLinking?: boolean; + readonly dependencies?: boolean; } export interface IProcessor { @@ -1240,8 +1241,12 @@ const defaultIgnore = [ const notIgnored = ['!package.json', '!README.md']; -function collectAllFiles(cwd: string, useYarn?: boolean, dependencyEntryPoints?: string[]): Promise { - return getDependencies(cwd, useYarn, dependencyEntryPoints).then(deps => { +function collectAllFiles( + cwd: string, + dependencies: 'npm' | 'yarn' | 'none' | undefined, + dependencyEntryPoints?: string[] +): Promise { + return getDependencies(cwd, dependencies, dependencyEntryPoints).then(deps => { const promises: Promise[] = deps.map(dep => { return glob('**', { cwd: dep, nodir: true, dot: true, ignore: 'node_modules/**' }).then(files => files.map(f => path.relative(cwd, path.join(dep, f))).map(f => f.replace(/\\/g, '/')) @@ -1254,11 +1259,11 @@ function collectAllFiles(cwd: string, useYarn?: boolean, dependencyEntryPoints?: function collectFiles( cwd: string, - useYarn?: boolean, + dependencies: 'npm' | 'yarn' | 'none' | undefined, dependencyEntryPoints?: string[], ignoreFile?: string ): Promise { - return collectAllFiles(cwd, useYarn, dependencyEntryPoints).then(files => { + return collectAllFiles(cwd, dependencies, dependencyEntryPoints).then(files => { files = files.filter(f => !/\r$/m.test(f)); return ( @@ -1337,13 +1342,31 @@ export function createDefaultProcessors(manifest: Manifest, options: IPackageOpt ]; } +function getDependenciesOption(options: { + readonly dependencies?: boolean; + readonly useYarn?: boolean; +}): 'npm' | 'yarn' | 'none' | undefined { + if (options.dependencies === false) { + return 'none'; + } + + switch (options.useYarn) { + case true: + return 'yarn'; + case false: + return 'npm'; + default: + return undefined; + } +} + export function collect(manifest: Manifest, options: IPackageOptions = {}): Promise { const cwd = options.cwd || process.cwd(); const packagedDependencies = options.dependencyEntryPoints || undefined; const ignoreFile = options.ignoreFile || undefined; const processors = createDefaultProcessors(manifest, options); - return collectFiles(cwd, options.useYarn, packagedDependencies, ignoreFile).then(fileNames => { + return collectFiles(cwd, getDependenciesOption(options), packagedDependencies, ignoreFile).then(fileNames => { const files = fileNames.map(f => ({ path: `extension/${f}`, localPath: path.join(cwd, f) })); return processFiles(processors, files); @@ -1463,30 +1486,36 @@ export async function packageCommand(options: IPackageOptions = {}): Promise { - await readManifest(cwd); - return await collectFiles(cwd, useYarn, packagedDependencies, ignoreFile); +export async function listFiles(options: IListFilesOptions = {}): Promise { + const cwd = options.cwd ?? process.cwd(); + const manifest = await readManifest(cwd); + + if (options.prepublish) { + await prepublish(cwd, manifest, options.useYarn); + } + + return await collectFiles(cwd, getDependenciesOption(options), options.packagedDependencies, options.ignoreFile); } /** * Lists the files included in the extension's package. Runs prepublish. */ -export function ls( - cwd = process.cwd(), - useYarn?: boolean, - packagedDependencies?: string[], - ignoreFile?: string -): Promise { - return readManifest(cwd) - .then(manifest => prepublish(cwd, manifest, useYarn)) - .then(() => collectFiles(cwd, useYarn, packagedDependencies, ignoreFile)) - .then(files => files.forEach(f => console.log(`${f}`))); +export async function ls(options: IListFilesOptions = {}): Promise { + const files = await listFiles({ ...options, prepublish: true }); + + for (const file of files) { + console.log(`${file}`); + } } diff --git a/src/publish.ts b/src/publish.ts index b817ba26..8fadbd5c 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -26,6 +26,7 @@ export interface IPublishOptions { readonly ignoreFile?: string; readonly pat?: string; readonly noVerify?: boolean; + readonly dependencies?: boolean; } export async function publish(options: IPublishOptions = {}): Promise { diff --git a/src/test/package.test.ts b/src/test/package.test.ts index cc5e79ec..fc67073f 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -223,6 +223,18 @@ describe('collect', function () { files.forEach(file => assert.ok(file.path.indexOf('/node_modules/which/') < 0, file.path)); }); }); + + it('should skip all dependencies when using --no-dependencies', async () => { + const cwd = fixture('devDependencies'); + const manifest = await readManifest(cwd); + const files = await collect(manifest, { cwd, dependencies: false }); + + assert.strictEqual(files.length, 3); + + for (const file of files) { + assert.ok(!/\bnode_modules\b/i.test(file.path)); + } + }); }); describe('readManifest', () => {