Skip to content

Commit

Permalink
feat: add --no-dependencies flag
Browse files Browse the repository at this point in the history
closes #439
  • Loading branch information
joaomoreno committed Nov 2, 2021
1 parent 8a749d9 commit d797b8d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 37 deletions.
8 changes: 2 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface IPublishOptions {
export enum PackageManager {
Npm,
Yarn,
None,
}

export interface IListFilesOptions {
Expand Down Expand Up @@ -138,12 +139,7 @@ export function publish(options: IPublishOptions = {}): Promise<any> {
* Lists the files included in the extension's package.
*/
export function listFiles(options: IListFilesOptions = {}): Promise<string[]> {
return _listFiles(
options.cwd,
options.packageManager === PackageManager.Yarn,
options.packagedDependencies,
options.ignoreFile
);
return _listFiles(options);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ module.exports = function (argv: string[]): void {
undefined
)
.option('--ignoreFile <path>', '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
Expand All @@ -96,6 +97,7 @@ module.exports = function (argv: string[]): void {
.option('--ignoreFile <path>', '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,
Expand All @@ -112,6 +114,7 @@ module.exports = function (argv: string[]): void {
ignoreFile,
gitHubIssueLinking,
gitLabIssueLinking,
dependencies,
}
) =>
main(
Expand All @@ -129,6 +132,7 @@ module.exports = function (argv: string[]): void {
ignoreFile,
gitHubIssueLinking,
gitLabIssueLinking,
dependencies,
})
)
);
Expand Down Expand Up @@ -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 <path>', 'Indicate alternative .vscodeignore')
.option('--no-dependencies', 'Disable dependency detection via npm or yarn')
.action(
(
version,
Expand All @@ -175,6 +180,7 @@ module.exports = function (argv: string[]): void {
yarn,
noVerify,
ignoreFile,
dependencies,
}
) =>
main(
Expand All @@ -192,6 +198,7 @@ module.exports = function (argv: string[]): void {
useYarn: yarn,
noVerify,
ignoreFile,
dependencies,
})
)
);
Expand Down
14 changes: 9 additions & 5 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
for (const name of ['yarn.lock', '.yarnrc', '.yarnrc.yaml', '.pnp.cjs', '.yarn']) {
if (await exists(path.join(cwd, name))) {
if (!process.env['VSCE_TESTS']) {
Expand All @@ -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<string[]> {
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<string> {
Expand Down
77 changes: 53 additions & 24 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface IPackageOptions {
readonly ignoreFile?: string;
readonly gitHubIssueLinking?: boolean;
readonly gitLabIssueLinking?: boolean;
readonly dependencies?: boolean;
}

export interface IProcessor {
Expand Down Expand Up @@ -1240,8 +1241,12 @@ const defaultIgnore = [

const notIgnored = ['!package.json', '!README.md'];

function collectAllFiles(cwd: string, useYarn?: boolean, dependencyEntryPoints?: string[]): Promise<string[]> {
return getDependencies(cwd, useYarn, dependencyEntryPoints).then(deps => {
function collectAllFiles(
cwd: string,
dependencies: 'npm' | 'yarn' | 'none' | undefined,
dependencyEntryPoints?: string[]
): Promise<string[]> {
return getDependencies(cwd, dependencies, dependencyEntryPoints).then(deps => {
const promises: Promise<string[]>[] = 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, '/'))
Expand All @@ -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<string[]> {
return collectAllFiles(cwd, useYarn, dependencyEntryPoints).then(files => {
return collectAllFiles(cwd, dependencies, dependencyEntryPoints).then(files => {
files = files.filter(f => !/\r$/m.test(f));

return (
Expand Down Expand Up @@ -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<IFile[]> {
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);
Expand Down Expand Up @@ -1463,30 +1486,36 @@ export async function packageCommand(options: IPackageOptions = {}): Promise<any
util.log.done(`Packaged: ${packagePath} (${files.length} files, ${size}${unit})`);
}

export interface IListFilesOptions {
readonly cwd?: string;
readonly useYarn?: boolean;
readonly packagedDependencies?: string[];
readonly ignoreFile?: string;
readonly dependencies?: boolean;
readonly prepublish?: boolean;
}

/**
* Lists the files included in the extension's package. Does not run prepublish.
* Lists the files included in the extension's package.
*/
export async function listFiles(
cwd = process.cwd(),
useYarn?: boolean,
packagedDependencies?: string[],
ignoreFile?: string
): Promise<string[]> {
await readManifest(cwd);
return await collectFiles(cwd, useYarn, packagedDependencies, ignoreFile);
export async function listFiles(options: IListFilesOptions = {}): Promise<string[]> {
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<void> {
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<void> {
const files = await listFiles({ ...options, prepublish: true });

for (const file of files) {
console.log(`${file}`);
}
}
1 change: 1 addition & 0 deletions src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
Expand Down
12 changes: 12 additions & 0 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit d797b8d

Please sign in to comment.