add dependencyEntryPoints-option, remove vscode:packagedDependencies

This commit is contained in:
Johannes Rieken 2018-08-30 15:11:00 +02:00
parent bf75ad47c9
commit 829273552f
5 changed files with 57 additions and 32 deletions

View file

@ -83,6 +83,13 @@ export interface IListFilesOptions {
* The package manager to use. Defaults to `PackageManager.Npm`.
*/
packageManager?: PackageManager;
/**
* A subset of the top level dependencies which should be included. The
* default is `undefined` which include all dependencies, an empty array means
* no dependencies will be included.
*/
packagedDependencies?: string[];
}
export interface IPublishVSIXOptions {
@ -128,7 +135,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);
return _listFiles(options.cwd, options.packageManager === PackageManager.Yarn, options.packagedDependencies);
}
/**

View file

@ -102,7 +102,7 @@ function asYarnDependency(prefix: string, tree: YarnTreeNode): YarnDependency |
return { name, version, path: dependencyPath, children };
}
function selectYarnDependencies(deps: YarnDependency[], entries: string[]): YarnDependency[] {
function selectYarnDependencies(deps: YarnDependency[], packagedDependencies: string[]): YarnDependency[] {
const index = new class {
private data: { [name: string]: YarnDependency } = Object.create(null);
@ -144,11 +144,11 @@ function selectYarnDependencies(deps: YarnDependency[], entries: string[]): Yarn
visit(child.name);
}
};
entries.forEach(visit);
packagedDependencies.forEach(visit);
return reached.values;
}
async function getYarnProductionDependencies(cwd: string): Promise<YarnDependency[]> {
async function getYarnProductionDependencies(cwd: string, packagedDependencies?: string[]): Promise<YarnDependency[]> {
const raw = await new Promise<string>((c, e) => cp.exec('yarn list --prod --json', { cwd, encoding: 'utf8', env: { ...process.env } }, (err, stdout) => err ? e(err) : c(stdout)));
const match = /^{"type":"tree".*$/m.exec(raw);
@ -162,25 +162,18 @@ async function getYarnProductionDependencies(cwd: string): Promise<YarnDependenc
.map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree))
.filter(dep => !!dep);
try {
let pkg = require(path.join(cwd, 'package.json'));
let entries = pkg['vscode:packagedDependencies'];
if (Array.isArray(entries) && entries.length > 0) {
result = selectYarnDependencies(result, entries);
}
} catch (err) {
console.log(err);
// ignore
if (Array.isArray(packagedDependencies)) {
result = selectYarnDependencies(result, packagedDependencies);
}
return result;
}
async function getYarnDependencies(cwd: string): Promise<string[]> {
async function getYarnDependencies(cwd: string, packagedDependencies?: string[]): Promise<string[]> {
const result: string[] = [cwd];
if (await new Promise(c => fs.exists(path.join(cwd, 'yarn.lock'), c))) {
const deps = await getYarnProductionDependencies(cwd);
const deps = await getYarnProductionDependencies(cwd, packagedDependencies);
const flatten = (dep: YarnDependency) => { result.push(dep.path); dep.children.forEach(flatten); };
deps.forEach(flatten);
}
@ -188,8 +181,8 @@ async function getYarnDependencies(cwd: string): Promise<string[]> {
return _.uniq(result);
}
export function getDependencies(cwd: string, useYarn = false): Promise<string[]> {
return useYarn ? getYarnDependencies(cwd) : getNpmDependencies(cwd);
export function getDependencies(cwd: string, useYarn = false, packagedDependencies?: string[]): Promise<string[]> {
return useYarn ? getYarnDependencies(cwd, packagedDependencies) : getNpmDependencies(cwd);
}
export function getLatestVersion(name: string, cancellationToken?: CancellationToken): Promise<string> {

View file

@ -63,6 +63,7 @@ export interface IPackageOptions {
baseContentUrl?: string;
baseImagesUrl?: string;
useYarn?: boolean;
packagedDependencies?: string[];
}
export interface IProcessor {
@ -685,8 +686,8 @@ const defaultIgnore = [
'**/*.vsixmanifest'
];
function collectAllFiles(cwd: string, useYarn = false): Promise<string[]> {
return getDependencies(cwd, useYarn).then(deps => {
function collectAllFiles(cwd: string, useYarn = false, dependencyEntryPoints?: string[]): Promise<string[]> {
return getDependencies(cwd, useYarn, dependencyEntryPoints).then(deps => {
const promises = deps.map(dep => {
return glob('**', { cwd: dep, nodir: true, dot: true, ignore: 'node_modules/**' })
.then(files => files
@ -698,8 +699,8 @@ function collectAllFiles(cwd: string, useYarn = false): Promise<string[]> {
});
}
function collectFiles(cwd: string, useYarn = false): Promise<string[]> {
return collectAllFiles(cwd, useYarn).then(files => {
function collectFiles(cwd: string, useYarn = false, dependencyEntryPoints?: string[]): Promise<string[]> {
return collectAllFiles(cwd, useYarn, dependencyEntryPoints).then(files => {
files = files.filter(f => !/\r$/m.test(f));
return readFile(path.join(cwd, '.vscodeignore'), 'utf8')
@ -757,9 +758,10 @@ export function createDefaultProcessors(manifest: Manifest, options: IPackageOpt
export function collect(manifest: Manifest, options: IPackageOptions = {}): Promise<IFile[]> {
const cwd = options.cwd || process.cwd();
const useYarn = options.useYarn || false;
const packagedDependencies = options.packagedDependencies || undefined;
const processors = createDefaultProcessors(manifest, options);
return collectFiles(cwd, useYarn).then(fileNames => {
return collectFiles(cwd, useYarn, packagedDependencies).then(fileNames => {
const files = fileNames.map(f => ({ path: `extension/${f}`, localPath: path.join(cwd, f) }));
return processFiles(processors, files, options);
@ -821,17 +823,17 @@ export function packageCommand(options: IPackageOptions = {}): Promise<any> {
/**
* Lists the files included in the extension's package. Does not run prepublish.
*/
export function listFiles(cwd = process.cwd(), useYarn = false): Promise<string[]> {
export function listFiles(cwd = process.cwd(), useYarn = false, packagedDependencies?: string[]): Promise<string[]> {
return readManifest(cwd)
.then(manifest => collectFiles(cwd, useYarn));
.then(manifest => collectFiles(cwd, useYarn, packagedDependencies));
}
/**
* Lists the files included in the extension's package. Runs prepublish.
*/
export function ls(cwd = process.cwd(), useYarn = false): Promise<void> {
export function ls(cwd = process.cwd(), useYarn = false, packagedDependencies?: string[]): Promise<void> {
return readManifest(cwd)
.then(manifest => prepublish(cwd, manifest))
.then(manifest => collectFiles(cwd, useYarn))
.then(manifest => collectFiles(cwd, useYarn, packagedDependencies))
.then(files => files.forEach(f => console.log(`${f}`)));
}

View file

@ -8,8 +8,5 @@
"dependencies": {
"isexe": "1.0.0",
"which": "1.3.1"
},
"vscode:packagedDependencies": [
"isexe"
]
}
}

View file

@ -177,7 +177,24 @@ describe('collect', function () {
});
});
it('should honor vscode:packagedDependencies', () => {
it('should honor dependencyEntryPoints', () => {
const cwd = fixture('packagedDependencies');
return readManifest(cwd)
.then(manifest => collect(manifest, { cwd, useYarn: true, packagedDependencies: ['isexe'] }))
.then(files => {
let seenWhich: boolean;
let seenIsexe: boolean;
files.forEach(file => {
seenWhich = file.path.indexOf('/node_modules/which/') >= 0;
seenIsexe = file.path.indexOf('/node_modules/isexe/') >= 0;
});
assert.equal(seenWhich, false);
assert.equal(seenIsexe, true);
});
});
it('should include all node_modules when dependencyEntryPoints is not defined', () => {
const cwd = fixture('packagedDependencies');
return readManifest(cwd)
@ -189,10 +206,19 @@ describe('collect', function () {
seenWhich = file.path.indexOf('/node_modules/which/') >= 0;
seenIsexe = file.path.indexOf('/node_modules/isexe/') >= 0;
});
assert.equal(seenWhich, false);
assert.equal(seenWhich, true);
assert.equal(seenIsexe, true);
});
});
it('should skip all node_modules when dependencyEntryPoints is []', () => {
const cwd = fixture('packagedDependencies');
return readManifest(cwd)
.then(manifest => collect(manifest, { cwd, useYarn: true, packagedDependencies: [] }))
.then(files => {
files.forEach(file => assert.ok(file.path.indexOf('/node_modules/which/') < 0, file.path));
});
});
});