Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit d17ea4d

Browse files
authored
refactor: support local install with npm 5 (#281)
* apply INIT_CWD for all bin scripts * invoke webpack with --preserve-symlinks
1 parent 2326891 commit d17ea4d

9 files changed

+41
-29
lines changed

Diff for: bin/generate-android-snapshot

100644100755
File mode changed.

Diff for: bin/install-ns-webpack

100644100755
File mode changed.

Diff for: bin/ns-bundle

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const { existsSync } = require("fs");
66

77
const semver = require("semver");
88

9-
const { getPackageJson } = require("../projectHelpers");
9+
const { getPackageJson, getProjectDir } = require("../projectHelpers");
1010

11-
const PROJECT_DIR = pathResolve(__dirname, "../../../");
11+
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
1212
const packageJson = getPackageJson(PROJECT_DIR);
1313

1414
if (!process.env.npm_config_argv) {
@@ -152,7 +152,9 @@ function webpack(platform, env) {
152152
console.log(`Running webpack for ${platform}...`);
153153

154154
const args = [
155-
`webpack`,
155+
`node`,
156+
`--preserve-symlinks`,
157+
`./node_modules/.bin/webpack`,
156158
`--config=webpack.config.js`,
157159
`--progress`,
158160
`--env.${platform}`,

Diff for: bin/ns-verify-bundle

100644100755
+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
const path = require("path");
44
const fs = require("fs");
55

6-
const PROJECT_DIR = path.resolve(__dirname, "../../../");
6+
const { getProjectDir } = require("../projectHelpers");
7+
8+
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
79
const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
810
const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
911
const PROJECT_PATHS = {

Diff for: bin/remove-ns-webpack

100644100755
File mode changed.

Diff for: bin/update-ns-webpack

100644100755
+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env node
22
const { resolve } = require("path");
33

4-
const { getPackageJson, writePackageJson } = require("../projectHelpers");
4+
const { getPackageJson, getProjectDir, writePackageJson } = require("../projectHelpers");
55
const { forceUpdateProjectDeps } = require("../dependencyManager");
66
const { editExistingProjectFiles } = require("../projectFilesManager");
77

8-
const PROJECT_DIR = resolve(__dirname, "../../../");
8+
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
9+
const packageJson = getPackageJson(PROJECT_DIR);
910

1011
console.info("Updating dev dependencies...");
11-
const packageJson = getPackageJson(PROJECT_DIR);
12+
1213
const { deps } = forceUpdateProjectDeps(packageJson);
1314
packageJson.devDependencies = deps;
1415
writePackageJson(packageJson, PROJECT_DIR);

Diff for: index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const path = require("path");
22
const { existsSync } = require("fs");
33

4-
const { getPackageJson, isAngular } = require("./projectHelpers");
4+
const { getPackageJson, getProjectDir, isAngular } = require("./projectHelpers");
55

6-
const PROJECT_DIR = path.dirname(path.dirname(__dirname));
6+
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
77
const APP_DIR = path.join(PROJECT_DIR, "app");
88

99
Object.assign(exports, require('./plugins'));

Diff for: installer.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,7 @@ const projectFilesManager = require("./projectFilesManager");
66
const npmScriptsManager = require("./npmScriptsManager");
77
const dependencyManager = require("./dependencyManager");
88

9-
// INIT_CWD is available since npm 5.4
10-
const initCwd = process.env.INIT_CWD;
11-
const shouldUseInitCwd = () => {
12-
if (!initCwd) {
13-
return false;
14-
}
15-
16-
const installedPackage = path.resolve(initCwd, "node_modules", "nativescript-dev-webpack");
17-
if (!fs.existsSync(installedPackage)) {
18-
return false;
19-
}
20-
21-
const stat = fs.lstatSync(installedPackage);
22-
return stat.isSymbolicLink();
23-
};
24-
25-
const PROJECT_DIR = shouldUseInitCwd() ?
26-
initCwd :
27-
path.dirname(path.dirname(__dirname));
9+
const PROJECT_DIR = helpers.getProjectDir({ nestingLvl: 2 });
2810
const APP_DIR = path.resolve(PROJECT_DIR, "app");
2911

3012
function install() {

Diff for: projectHelpers.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,36 @@ const writePackageJson = (content, projectDir) => {
3030
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
3131
}
3232

33+
const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
34+
// INIT_CWD is available since npm 5.4
35+
const initCwd = process.env.INIT_CWD;
36+
const shouldUseInitCwd = (() => {
37+
if (!initCwd) {
38+
return false;
39+
}
40+
41+
const installedPackage = path.resolve(initCwd, "node_modules", "nativescript-dev-webpack");
42+
if (!fs.existsSync(installedPackage)) {
43+
return false;
44+
}
45+
46+
const stat = fs.lstatSync(installedPackage);
47+
return stat.isSymbolicLink();
48+
})();
49+
50+
return shouldUseInitCwd ?
51+
initCwd :
52+
Array
53+
.from(Array(nestingLvl))
54+
.reduce(dir => path.dirname(dir), __dirname);
55+
};
56+
3357
const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json");
3458

3559
module.exports = {
3660
isTypeScript,
3761
isAngular,
38-
getPackageJson,
3962
writePackageJson,
63+
getPackageJson,
64+
getProjectDir,
4065
};

0 commit comments

Comments
 (0)