Skip to content

Commit

Permalink
feat(common-cli): add common-cli to share code among our create-*
Browse files Browse the repository at this point in the history
… CLI packages
  • Loading branch information
tinesoft committed Oct 6, 2023
1 parent bb6a5ab commit bcb5fd2
Show file tree
Hide file tree
Showing 27 changed files with 482 additions and 581 deletions.
1 change: 1 addition & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
}
},
"workspaceLayout": {
"projectNameAndRootFormat": "derived",
"appsDir": "e2e",
"libsDir": "packages"
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"hpagent": "^1.2.0",
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.12",
"terminal-link": "^2.1.1",
"tslib": "^2.6.1",
"unzipper": "^0.10.14",
"xmlbuilder2": "^3.1.1",
Expand Down
25 changes: 25 additions & 0 deletions packages/common-cli/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
15 changes: 15 additions & 0 deletions packages/common-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# common-cli

Common library to share code among the `create-*` packages.

## Building

Run `nx build common-cli` to build the library.

## Running unit tests

Run `nx test common-cli` to execute the unit tests via [Jest](https://jestjs.io).

## License

Copyright (c) 2023-present Tine Kondo. Licensed under the MIT License (MIT)
11 changes: 11 additions & 0 deletions packages/common-cli/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'common-cli',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/common-cli',
};
42 changes: 42 additions & 0 deletions packages/common-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@nxrocks/common-cli",
"description": "Common library to share code among the `create-*` packages.",
"version": "0.0.0-development",
"private": false,
"publishConfig": {
"access": "public"
},
"main": "src/index.js",
"license": "MIT",
"author": "Tine Kondo ",
"repository": {
"type": "git",
"url": "https://github.com/tinesoft/nxrocks.git",
"directory": "packages/common-cli"
},
"homepage": "https://github.com/tinesoft/nxrocks/blob/master/packages/common-cli#readme",
"bugs": {
"url": "https://github.com/tinesoft/nxrocks/issues"
},
"keywords": [
"create-nx-flutter",
"create-nx-spring-boot",
"create-nx-quarkus",
"create-nx-micronaut",
"create-nx-ktor"
],
"dependencies": {
"create-nx-workspace": "16.9.0",
"@clack/prompts": "^0.7.0",
"yargs": "^17.7.2",
"tslib": "^2.6.1",
"@nx/devkit": "16.9.0",
"fs-extra": "^10.1.0",
"terminal-link": "^2.1.1"
},
"typings": "./src/index.d.ts",
"peerDependencies": {
},
"devDependencies": {
}
}
47 changes: 47 additions & 0 deletions packages/common-cli/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "common-cli",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/common-cli/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/common-cli",
"main": "packages/common-cli/src/index.ts",
"tsConfig": "packages/common-cli/tsconfig.lib.json",
"assets": ["packages/common-cli/*.md"]
}
},
"publish": {
"command": "node tools/scripts/publish.mjs common-cli {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/common-cli/**/*.ts",
"packages/common-cli/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/common-cli/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
3 changes: 3 additions & 0 deletions packages/common-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './lib/utils';
export * from './lib/models';
export * from './lib/main';
108 changes: 108 additions & 0 deletions packages/common-cli/src/lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { intro, text, confirm, spinner, note, outro } from '@clack/prompts';
import { createWorkspace } from 'create-nx-workspace';
import * as yargs from 'yargs';
import * as terminalLink from 'terminal-link';

import { CLIArguments } from './models';
import { createWorkspaceWithNxWrapper, getNxCommand } from './utils';

export async function mainCLI(pkgName: string, stackName: string) {

const pkgFolderName = pkgName.replace('@nxrocks/', '')
const createPkgName = `create-${pkgFolderName}`;

intro(createPkgName);

const options = (yargs
.parserConfiguration({
'strip-dashed': true,
})
.command(
'$0 [name]',
`Create a new Nx workspace with ${stackName} support`,
(yargs) => yargs.option('name',
{
describe: 'Workspace name (e.g. org name)',
type: 'string',
}).
option('useNxWrapper',
{
describe: 'Let Nx manages its own installation and updates',
type: 'boolean',
}).
option('useNxCloud',
{
describe: "Enable distributed caching to make your CI faster",
type: 'boolean',
}).
option('verbose',
{
describe: "Enable more logging information",
type: 'boolean',
default: process.env['NX_VERBOSE_LOGGING'] === 'true',
})
)
.help('help', 'Show help') as yargs.Argv<CLIArguments>).parseSync();

let { name, useNxWrapper, useNxCloud } = options;
const { _, $0, name: ignoredName, verbose, ...restArgs } = options;

name ||= await text({
message: 'What is the name of your workspace (e.g. org name)?',
initialValue: 'myorg',
validate: (value) => (value?.length === 0) ? 'You need to provide one' : void 0,
}) as string;

useNxWrapper ??= await confirm({
message: `Would you like to use Nx Wrapper? [ ${linkify('Nx Wrapper', 'https://nx.dev/concepts/more-concepts/nx-and-the-wrapper#and-the-nx-wrapper')} ]`,
initialValue: true
}) as boolean;

useNxCloud ??= await confirm({
message: `Would you like to use Nx Cloud? [ ${linkify('Nx Cloud', 'https://nx.app/?utm_source=' + createPkgName)} ]`,
initialValue: false
}) as boolean;

const presetVersion = 'latest';

let directory: string;

const notes = [
`- Go to [ ${linkify(pkgName, 'https://github.com/tinesoft/nxrocks/tree/develop/packages/' + pkgFolderName)} ] to get started with Nx and ${stackName} plugin.`,
`- Run [ ${getNxCommand(useNxWrapper)} g ${pkgName}:project ] to add more projects.`
];

if (useNxWrapper) {
notes.push(`- Go to [ ${linkify('Nx Wrapper', 'https://nx.dev/concepts/more-concepts/nx-and-the-wrapper#and-the-nx-wrapper')} ] to get started with Nx Wrapper.`);

const allArgs = Object.entries(restArgs).map(([key, value]) => `--${key}=${value}`).join(' ');

const s = spinner();
s.start('Initializing your workspace');

directory = createWorkspaceWithNxWrapper(name, pkgName, allArgs, useNxCloud, presetVersion, !verbose);

s.stop(`Successfully created the workspace: ${name}`);
}
else {
directory = (await createWorkspace(
`${pkgName}@${presetVersion}`,
{
...restArgs,
name: name,
nxCloud: useNxCloud,
packageManager: 'npm'
}
))?.directory;
}

notes.push(`- Go to [ ${linkify('Nx.dev', 'https://nx.dev')} ] to get started with Nx.`);

note(notes.join('\n'), "Next steps");

outro(`Your workspace in ${directory} is all set 🎉. Let's goooooo! 🚀`);
}

function linkify(text: string, url: string, fallback = (text: string, url: string) => url) {
return terminalLink(text, url, { fallback });
}
13 changes: 13 additions & 0 deletions packages/common-cli/src/lib/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CreateWorkspaceOptions } from 'create-nx-workspace';


export interface CLIArguments extends CreateWorkspaceOptions {
name: string;
useNxWrapper?: boolean;
useNxCloud?: boolean;
interactive: boolean;
verbose: boolean;
$0?: string;
_?: string[]
}

Loading

0 comments on commit bcb5fd2

Please sign in to comment.