Skip to content

Commit

Permalink
Merge branch 'SrTobi-full-npm-dep-ignore'
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Jan 28, 2016
2 parents e3cafa9 + 69e2c7d commit 1e1887d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as path from 'path';
import * as child_process from 'child_process';

const cmd = 'npm list --production --parseable --depth';

export function getDependencies(cwd: string): Promise<string[]> {
return new Promise<string[]>((c, e) => {
child_process.exec(cmd, { cwd }, (err, stdout, stderr) => {
if (err) return e(err);

c(stdout.toString('utf8')
.split(/[\r\n]/)
.filter(dir => path.isAbsolute(dir)));
}
);
});
}
18 changes: 13 additions & 5 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as denodeify from 'denodeify';
import * as mime from 'mime';
import * as urljoin from 'url-join';
import { validatePublisher, validateExtensionName, validateVersion } from './validation';
import { getDependencies } from './npm';

interface IReadFile {
(filePath: string): Promise<Buffer>;
Expand Down Expand Up @@ -304,17 +305,24 @@ const defaultIgnore = [
'**/*.vsixmanifest'
];

function devDependenciesIgnore(manifest: Manifest): string[] {
const devDependencies = Object.keys(manifest.devDependencies || {});
return devDependencies.map(d => `node_modules/${ d }/**`);
function collectAllFiles(cwd: string): Promise<string[]> {
return getDependencies(cwd).then(deps => {
const promises = 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, '/')));
});

return Promise.all(promises).then(util.flatten);
});
}

function collectFiles(cwd: string, manifest: Manifest): Promise<string[]> {
return glob('**', { cwd, nodir: true, dot: true }).then(files => {
return collectAllFiles(cwd).then(files => {
return readFile(path.join(cwd, '.vscodeignore'), 'utf8')
.catch<string>(err => err.code !== 'ENOENT' ? Promise.reject(err) : Promise.resolve(''))
.then(rawIgnore => rawIgnore.split(/[\n\r]/).map(s => s.trim()).filter(s => !!s))
.then(ignore => devDependenciesIgnore(manifest).concat(ignore))
.then(ignore => defaultIgnore.concat(ignore))
.then(ignore => ignore.filter(i => !/^\s*#/.test(i)))
.then<{ ignore: string[]; negate: string[]; }>(ignore => <any> _.indexBy(_.partition(ignore, i => !/^\s*!/.test(i)), (o, i) => i ? 'negate' : 'ignore'))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/test/fixtures/devDependencies/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "uuid",
"name": "root-test-package",
"publisher": "joaomoreno",
"version": "1.0.0",
"engines": { "vscode": "*" },
Expand Down
3 changes: 1 addition & 2 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ describe('collect', () => {

it('should ignore devDependencies', () => {
const cwd = fixture('devDependencies');

return readManifest(cwd)
.then(manifest => collect(manifest, { cwd }))
.then(files => {
assert.equal(files.length, 4);
assert.equal(files.length, 5);
assert.ok(files.some(f => /real\/dependency\.js/.test(f.path)));
assert.ok(!files.some(f => /fake\/dependency\.js/.test(f.path)));
});
Expand Down
6 changes: 5 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ function chain2<A,B>(a: A, b: B[], fn: (a: A, b: B)=>Promise<A>, index = 0): Pro
if (index >= b.length) {
return Promise.resolve(a);
}

return fn(a, b[index]).then(a => chain2(a, b, fn, index + 1));
}

export function chain<T,P>(initial: T, processors: P[], process: (a: T, b: P)=>Promise<T>): Promise<T> {
return chain2(initial, processors, process);
}

export function flatten<T>(arr: T[][]): T[] {
return [].concat.apply([], arr) as T[];
}

0 comments on commit 1e1887d

Please sign in to comment.