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

refactor: support local install with npm 5 #281

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/generate-android-snapshot
100644 → 100755
Empty file.
Empty file modified bin/install-ns-webpack
100644 → 100755
Empty file.
8 changes: 5 additions & 3 deletions bin/ns-bundle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const { existsSync } = require("fs");

const semver = require("semver");

const { getPackageJson } = require("../projectHelpers");
const { getPackageJson, getProjectDir } = require("../projectHelpers");

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

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

const args = [
`webpack`,
`node`,
`--preserve-symlinks`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

`./node_modules/.bin/webpack`,
`--config=webpack.config.js`,
`--progress`,
`--env.${platform}`,
Expand Down
4 changes: 3 additions & 1 deletion bin/ns-verify-bundle
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const path = require("path");
const fs = require("fs");

const PROJECT_DIR = path.resolve(__dirname, "../../../");
const { getProjectDir } = require("../projectHelpers");

const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
const PROJECT_PATHS = {
Expand Down
Empty file modified bin/remove-ns-webpack
100644 → 100755
Empty file.
7 changes: 4 additions & 3 deletions bin/update-ns-webpack
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env node
const { resolve } = require("path");

const { getPackageJson, writePackageJson } = require("../projectHelpers");
const { getPackageJson, getProjectDir, writePackageJson } = require("../projectHelpers");
const { forceUpdateProjectDeps } = require("../dependencyManager");
const { editExistingProjectFiles } = require("../projectFilesManager");

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

console.info("Updating dev dependencies...");
const packageJson = getPackageJson(PROJECT_DIR);

const { deps } = forceUpdateProjectDeps(packageJson);
packageJson.devDependencies = deps;
writePackageJson(packageJson, PROJECT_DIR);
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require("path");
const { existsSync } = require("fs");

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

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

Object.assign(exports, require('./plugins'));
Expand Down
20 changes: 1 addition & 19 deletions installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,7 @@ const projectFilesManager = require("./projectFilesManager");
const npmScriptsManager = require("./npmScriptsManager");
const dependencyManager = require("./dependencyManager");

// INIT_CWD is available since npm 5.4
const initCwd = process.env.INIT_CWD;
const shouldUseInitCwd = () => {
if (!initCwd) {
return false;
}

const installedPackage = path.resolve(initCwd, "node_modules", "nativescript-dev-webpack");
if (!fs.existsSync(installedPackage)) {
return false;
}

const stat = fs.lstatSync(installedPackage);
return stat.isSymbolicLink();
};

const PROJECT_DIR = shouldUseInitCwd() ?
initCwd :
path.dirname(path.dirname(__dirname));
const PROJECT_DIR = helpers.getProjectDir({ nestingLvl: 2 });
const APP_DIR = path.resolve(PROJECT_DIR, "app");

function install() {
Expand Down
27 changes: 26 additions & 1 deletion projectHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,36 @@ const writePackageJson = (content, projectDir) => {
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
}

const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be useful: https://www.npmjs.com/package/app-root-path or may be not. :)

// INIT_CWD is available since npm 5.4
const initCwd = process.env.INIT_CWD;
const shouldUseInitCwd = (() => {
if (!initCwd) {
return false;
}

const installedPackage = path.resolve(initCwd, "node_modules", "nativescript-dev-webpack");
if (!fs.existsSync(installedPackage)) {
return false;
}

const stat = fs.lstatSync(installedPackage);
return stat.isSymbolicLink();
})();

return shouldUseInitCwd ?
initCwd :
Array
.from(Array(nestingLvl))
.reduce(dir => path.dirname(dir), __dirname);
};

const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json");

module.exports = {
isTypeScript,
isAngular,
getPackageJson,
writePackageJson,
getPackageJson,
getProjectDir,
};