little refactor
This commit is contained in:
parent
e5f21a782d
commit
69e2c7de57
4 changed files with 37 additions and 48 deletions
17
src/npm.ts
Normal file
17
src/npm.ts
Normal file
|
@ -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)));
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
import * as pth from 'path';
|
||||
import * as _glob from 'glob';
|
||||
import * as denodeify from 'denodeify';
|
||||
import * as child_process from 'child_process';
|
||||
|
||||
const glob = denodeify<string, _glob.IOptions, string[]>(_glob);
|
||||
|
||||
|
||||
function flatten<T>(arr: T[][]): T[] {
|
||||
return [].concat.apply([], arr) as T[];
|
||||
}
|
||||
|
||||
function globFiles(cwd: string, moduleDirs: string[]): Promise<string[]> {
|
||||
return Promise.all(moduleDirs.map(dir => glob('**', { cwd: dir, nodir: true, dot: true, ignore: "node_modules/**" })
|
||||
.then(filepaths => {
|
||||
return filepaths.map(filepath => pth.relative(cwd, pth.join(dir, filepath)))
|
||||
.map(filepath => filepath.replace(/\\/g, '/'))
|
||||
})))
|
||||
.then(moduleFiles => flatten(moduleFiles));
|
||||
}
|
||||
|
||||
function npmProdDeps(cwd: string): Promise<string[]> {
|
||||
return new Promise<string[]>((resolve, reject) => {
|
||||
child_process.exec("npm list --production --parseable --depth", {cwd: cwd},
|
||||
(error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
|
||||
let moduleDirs = stdout.toString()
|
||||
.split("\n")
|
||||
.filter(dir => pth.isAbsolute(dir));
|
||||
|
||||
resolve(moduleDirs);
|
||||
}
|
||||
)
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
export function getDependencyFiles(cwd: string): Promise<string[]> {
|
||||
return npmProdDeps(cwd)
|
||||
.then(ps => globFiles(cwd, ps));
|
||||
}
|
|
@ -11,7 +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 {getDependencyFiles} from './npmdeps';
|
||||
import { getDependencies } from './npm';
|
||||
|
||||
interface IReadFile {
|
||||
(filePath: string): Promise<Buffer>;
|
||||
|
@ -305,9 +305,21 @@ const defaultIgnore = [
|
|||
'**/*.vsixmanifest'
|
||||
];
|
||||
|
||||
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 getDependencyFiles(cwd)
|
||||
.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))
|
||||
|
|
|
@ -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[];
|
||||
}
|
Loading…
Add table
Reference in a new issue