Skip to content

Commit

Permalink
feat(misc): move create-nx-plugin create script into create-nx-worksp…
Browse files Browse the repository at this point in the history
…ace package
  • Loading branch information
vsavkin committed Jan 27, 2020
1 parent 77238e2 commit 6706a05
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as path from 'path';
import { execSync } from 'child_process';
import * as inquirer from 'inquirer';
import yargsParser = require('yargs-parser');
import { determinePackageManager, showNxWarning } from './shared';

const tsVersion = 'TYPESCRIPT_VERSION';
const cliVersion = 'NX_VERSION';
Expand All @@ -29,7 +30,7 @@ if (parsedArgs.help) {

const packageManager = determinePackageManager();
determineWorkspaceName(parsedArgs).then(workspaceName => {
return determinPluginName(parsedArgs).then(pluginName => {
return determinePluginName(parsedArgs).then(pluginName => {
const tmpDir = createSandbox(packageManager);
createWorkspace(tmpDir, packageManager, parsedArgs, workspaceName);
createNxPlugin(workspaceName, pluginName);
Expand Down Expand Up @@ -113,72 +114,6 @@ function commitChanges(workspaceName) {
});
}

function showNxWarning(workspaceName: string) {
try {
const pathToRunNxCommand = path.resolve(process.cwd(), workspaceName);
execSync('nx --version', {
cwd: pathToRunNxCommand,
stdio: ['ignore', 'ignore', 'ignore']
});
} catch (e) {
// no nx found
output.addVerticalSeparator();
output.note({
title: `Nx CLI is not installed globally.`,
bodyLines: [
`This means that you might have to use "yarn nx" or "npm nx" to execute commands in the workspace.`,
`Run "yarn global add @nrwl/cli" or "npm install -g @nrwl/cli" to be able to execute command directly.`
]
});
}
}

// Move to @nrwl/workspace package?
function determinePackageManager() {
let packageManager = getPackageManagerFromAngularCLI();
if (packageManager === 'npm' || isPackageManagerInstalled(packageManager)) {
return packageManager;
}

if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}

if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}

return 'npm';
}

function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
// If it isn't installed, default to 'yarn'.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500
})
.toString()
.trim();
} catch (e) {
return 'yarn';
}
}

function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore']
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}

function determineWorkspaceName(parsedArgs: any): Promise<string> {
const workspaceName: string = parsedArgs._[2];

Expand Down Expand Up @@ -206,7 +141,7 @@ function determineWorkspaceName(parsedArgs: any): Promise<string> {
});
}

function determinPluginName(parsedArgs) {
function determinePluginName(parsedArgs) {
if (parsedArgs.pluginName) {
return Promise.resolve(parsedArgs.pluginName);
}
Expand Down Expand Up @@ -239,7 +174,7 @@ function showHelp() {
Args:
name workspace name
name workspace name (e.g., org name)
Options:
Expand Down
68 changes: 2 additions & 66 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as inquirer from 'inquirer';
import * as path from 'path';
import { dirSync } from 'tmp';
import * as yargsParser from 'yargs-parser';
import { determinePackageManager, showNxWarning } from './shared';

enum Preset {
Empty = 'empty',
Expand Down Expand Up @@ -109,7 +110,7 @@ function showHelp() {
Options:
name workspace name
name workspace name (e.g., org name)
preset What to create in a new workspace (options: ${options})
Expand All @@ -126,51 +127,6 @@ function showHelp() {
`);
}

function determinePackageManager() {
let packageManager = getPackageManagerFromAngularCLI();
if (packageManager === 'npm' || isPackageManagerInstalled(packageManager)) {
return packageManager;
}

if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}

if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}

return 'npm';
}

function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
// If it isn't installed, default to 'yarn'.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500
})
.toString()
.trim();
} catch (e) {
return 'yarn';
}
}

function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore']
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}

function determineWorkspaceName(parsedArgs: any): Promise<string> {
const workspaceName: string = parsedArgs._[2];

Expand Down Expand Up @@ -464,26 +420,6 @@ function createApp(
);
}

function showNxWarning(workspaceName: string) {
try {
const pathToRunNxCommand = path.resolve(process.cwd(), workspaceName);
execSync('nx --version', {
cwd: pathToRunNxCommand,
stdio: ['ignore', 'ignore', 'ignore']
});
} catch (e) {
// no nx found
output.addVerticalSeparator();
output.note({
title: `Nx CLI is not installed globally.`,
bodyLines: [
`This means that you might have to use "yarn nx" or "npm nx" to execute commands in the workspace.`,
`Run "yarn global add @nrwl/cli" or "npm install -g @nrwl/cli" to be able to execute command directly.`
]
});
}
}

function showCliWarning(preset: Preset, parsedArgs: yargsParser.Arguments) {
if (!parsedArgs.cli) {
switch (preset) {
Expand Down
68 changes: 68 additions & 0 deletions packages/create-nx-workspace/bin/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as path from 'path';
import { execSync } from 'child_process';
import { output } from '@nrwl/workspace/src/utils/output';

export function showNxWarning(workspaceName: string) {
try {
const pathToRunNxCommand = path.resolve(process.cwd(), workspaceName);
execSync('nx --version', {
cwd: pathToRunNxCommand,
stdio: ['ignore', 'ignore', 'ignore']
});
} catch (e) {
// no nx found
output.addVerticalSeparator();
output.note({
title: `Nx CLI is not installed globally.`,
bodyLines: [
`This means that you might have to use "yarn nx" or "npm nx" to execute commands in the workspace.`,
`Run "yarn global add @nrwl/cli" or "npm install -g @nrwl/cli" to be able to execute command directly.`
]
});
}
}

export function determinePackageManager() {
let packageManager = getPackageManagerFromAngularCLI();
if (packageManager === 'npm' || isPackageManagerInstalled(packageManager)) {
return packageManager;
}

if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}

if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}

return 'npm';
}

function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
// If it isn't installed, default to 'yarn'.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500
})
.toString()
.trim();
} catch (e) {
return 'yarn';
}
}

function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore']
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}
3 changes: 2 additions & 1 deletion packages/create-nx-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"CLI"
],
"bin": {
"create-nx-workspace": "./bin/create-nx-workspace.js"
"create-nx-workspace": "./bin/create-nx-workspace.js",
"create-nx-plugin": "./bin/create-nx-plugin.js"
},
"author": "Victor Savkin",
"license": "MIT",
Expand Down
10 changes: 2 additions & 8 deletions packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
{
"name": "@nrwl/nx-plugin",
"version": "0.0.1",
"description": "Node Plugin for Nx",
"description": "Plugin for creating plugins for Nx :)",
"repository": {
"type": "git",
"url": "git+https://github.com/nrwl/nx.git"
},
"keywords": [
"Monorepo",
"Node",
"Nest",
"Jest",
"Cypress",
"Nx",
"CLI"
],
"main": "./index.js",
"types": "./index.d.ts",
"bin": "./bin/create.js",
"author": "Nrwl",
"license": "MIT",
"bugs": {
Expand All @@ -35,8 +31,6 @@
"dependencies": {
"@nrwl/node": "*",
"@nrwl/linter": "*",
"@nrwl/workspace": "*",
"@nrwl/tao": "*",
"@angular-devkit/architect": "0.803.23",
"@angular-devkit/core": "8.3.23",
"@angular-devkit/schematics": "8.3.23",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rm -rf build/packages/angular/bundles/nrwl-angular-testing.umd.min.js.bak

rsync -a --exclude=*.ts packages/ build/packages
chmod +x build/packages/create-nx-workspace/bin/create-nx-workspace.js
chmod +x build/packages/nx-plugin/bin/create.js
chmod +x build/packages/create-nx-workspace/bin/create-nx-plugin.js
chmod +x build/packages/cli/bin/nx.js
chmod +x build/packages/tao/index.js

Expand Down
16 changes: 8 additions & 8 deletions scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "" "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "" "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "" "s|NX_VERSION|$NX_VERSION|g" nx-plugin/bin/create.js
sed -i "" "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" nx-plugin/bin/create.js
sed -i "" "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" nx-plugin/bin/create.js
sed -i "" "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" nx-plugin/bin/create.js
sed -i "" "s|NX_VERSION|$NX_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "" "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "" "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "" "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
else
sed -i "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" {react,next,web,jest,node,express,nest,cypress,storybook,angular,workspace}/src/utils/versions.js
sed -i "s|\*|$NX_VERSION|g" {schematics,react,next,web,jest,node,express,nest,cypress,storybook,angular,workspace,cli,linter,bazel,tao,eslint-plugin-nx,create-nx-workspace,nx-plugin}/package.json
sed -i "s|NX_VERSION|$NX_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js
sed -i "s|NX_VERSION|$NX_VERSION|g" nx-plugin/bin/create.js
sed -i "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" nx-plugin/bin/create.js
sed -i "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" nx-plugin/bin/create.js
sed -i "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" nx-plugin/bin/create.js
sed -i "s|NX_VERSION|$NX_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "s|TYPESCRIPT_VERSION|$TYPESCRIPT_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
sed -i "s|PRETTIER_VERSION|$PRETTIER_VERSION|g" create-nx-workspace/bin/create-nx-plugin.js
fi

if [[ $NX_VERSION == "*" ]]; then
Expand Down

0 comments on commit 6706a05

Please sign in to comment.