|
1 | 1 | import { copy } from "fs-extra-p"
|
2 | 2 | import { Minimatch } from "minimatch"
|
3 |
| -import { exec } from "./util" |
4 | 3 | import * as path from "path"
|
| 4 | +import { Promise as BluebirdPromise } from "bluebird" |
5 | 5 |
|
6 | 6 | //noinspection JSUnusedLocalSymbols
|
7 | 7 | const __awaiter = require("./awaiter")
|
| 8 | +const readInstalled = require("read-installed") |
8 | 9 |
|
9 | 10 | // we use relative path to avoid canonical path issue - e.g. /tmp vs /private/tmp
|
10 | 11 | export function copyFiltered(src: string, destination: string, filter: (file: string) => boolean, dereference: boolean): Promise<any> {
|
@@ -54,29 +55,43 @@ export function createFilter(src: string, patterns: Array<Minimatch>, ignoreFile
|
54 | 55 | }
|
55 | 56 | }
|
56 | 57 |
|
57 |
| -export async function listDependencies(appDir: string, production: boolean): Promise<Array<string>> { |
58 |
| - let npmExecPath = process.env.npm_execpath || process.env.NPM_CLI_JS |
59 |
| - const npmExecArgs = ["ls", production ? "--production" : "--dev", "--parseable"] |
60 |
| - if (npmExecPath == null) { |
61 |
| - npmExecPath = process.platform === "win32" ? "npm.cmd" : "npm" |
62 |
| - } |
63 |
| - else { |
64 |
| - npmExecArgs.unshift(npmExecPath) |
65 |
| - npmExecPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node" |
66 |
| - } |
| 58 | +export function devDependencies(dir: string): Promise<Array<string>> { |
| 59 | + return new BluebirdPromise((resolve, reject) => { |
| 60 | + readInstalled(dir, (error: Error, data: any) => { |
| 61 | + if (error) { |
| 62 | + reject(error) |
| 63 | + } |
| 64 | + else { |
| 65 | + resolve(flatDependencies(data, new Set())) |
| 66 | + } |
| 67 | + }) |
| 68 | + }) |
| 69 | +} |
67 | 70 |
|
68 |
| - const result = (await exec(npmExecPath, npmExecArgs, { |
69 |
| - cwd: appDir, |
70 |
| - stdio: "inherit", |
71 |
| - maxBuffer: 1024 * 1024, |
72 |
| - })).trim().split("\n") |
73 |
| - if (result.length > 0 && !result[0].includes("/node_modules/")) { |
74 |
| - // first line is a project dir |
75 |
| - const lastIndex = result.length - 1 |
76 |
| - result[0] = result[lastIndex] |
77 |
| - result.length = result.length - 1 |
| 71 | +function flatDependencies(data: any, seen: Set<string>): any { |
| 72 | + const deps = data.dependencies |
| 73 | + if (deps == null) { |
| 74 | + return [] |
78 | 75 | }
|
79 |
| - return result |
| 76 | + |
| 77 | + return Object.keys(deps).map(function (d) { |
| 78 | + if (typeof deps[d] !== "object" || seen.has(deps[d])) { |
| 79 | + return null |
| 80 | + } |
| 81 | + |
| 82 | + seen.add(deps[d]) |
| 83 | + if (deps[d].extraneous) { |
| 84 | + const extra = deps[d] |
| 85 | + delete deps[d] |
| 86 | + return extra.path |
| 87 | + } |
| 88 | + return flatDependencies(deps[d], seen) |
| 89 | + }) |
| 90 | + .filter(it => it !== null) |
| 91 | + .reduce(function FLAT(l, r) { |
| 92 | + return l.concat(Array.isArray(r) ? r.reduce(FLAT, []) : r) |
| 93 | + }, []) |
| 94 | + |
80 | 95 | }
|
81 | 96 |
|
82 | 97 | // https://github.com/joshwnj/minimatch-all/blob/master/index.js
|
|
0 commit comments