Skip to content

Commit

Permalink
feat(create-nx-flutter): add custom CLI to create Flutter projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Oct 6, 2023
1 parent 882d382 commit 5126425
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 0 deletions.
37 changes: 37 additions & 0 deletions e2e/nx-flutter-e2e/tests/create-nx-flutter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { hasNxWrapper, isNxWrapperInstalled } from '@nxrocks/common';
import { createCLITestProject } from '@nxrocks/common/testing';
import { execSync } from 'child_process';
import { rmSync } from 'fs';
import { join } from 'path';

describe('create-nx-flutter', () => {
let projectDirectory: string;

afterAll(() => {
// Cleanup the test project
rmSync(projectDirectory, {
recursive: true,
force: true,
});
});

it.each`
useNxWrapper
${true}
${false}
`('should be installed with Nx Wrapper=$useNxWrapper', ({useNxWrapper}) => {
projectDirectory = createCLITestProject('create-nx-flutter', `--prjName=bootapp --useNxWrapper=${useNxWrapper} --useNxCloud=false --no-interactive`);

// npm ls will fail if the package is not installed properly
execSync('npm ls @nxrocks/nx-flutter', {
cwd: useNxWrapper ? join(projectDirectory, '/.nx/installation'): projectDirectory,
stdio: 'inherit',
});

expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper);

if(useNxWrapper){
expect(isNxWrapperInstalled(projectDirectory)).toBe(true)
}
});
});
25 changes: 25 additions & 0 deletions packages/create-nx-flutter/.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"
}
}
]
}
29 changes: 29 additions & 0 deletions packages/create-nx-flutter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# create-nx-flutter

> Our very own **CLI** to create [Nx](https://nx.dev) workspaces with built-in support for [Micronaut](https://flutter.dev)!
The goal of this CLI is to ease the process of creating a Nx workspace that can host and manage Micronaut projects, thanks to our [@nxrocks/nx-flutter](https://github.com/tinesoft/nxrocks/blob/develop/packages/nx-flutter) plugin, that is automatically installed within it.

## Prerequisites

To run this CLI, all you need is to have [NodeJS](https://nodejs.org/en/download) installed (preferably the current LTS version).
Then to later create and run your Micronaut projets, you'll need [Flutter CLI](https://docs.flutter.dev/get-started/install) as well.

- **NodeJS** (preferably latest or LTS version) in path
- **Flutter CLI** in path

## Getting Started

Run `npx create-nx-flutter your-workspace-name` to execute the CLI.

This command will guide you through the creation process steps by steps.

## Options

Run `npx create-nx-flutter --help` to print some helpful info on available options.


## License

Copyright (c) 2023-present Tine Kondo. Licensed under the MIT License (MIT)

113 changes: 113 additions & 0 deletions packages/create-nx-flutter/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env node

import { intro, text, confirm, spinner, note, outro } from '@clack/prompts';
import { CreateWorkspaceOptions, createWorkspace } from 'create-nx-workspace';
import * as yargs from 'yargs';
import { createWorkspaceWithNxWrapper, getNxCommand } from '@nxrocks/common';

interface CLIArguments extends CreateWorkspaceOptions {
name: string;
useNxWrapper: boolean;
useNxCloud: boolean;
interactive: boolean,
verbose: boolean
}

async function main() {

intro('create-nx-flutter');

const options = (yargs
.parserConfiguration({
'strip-dashed': true,
})
.command(
'$0 [name]',
'Create a new Nx workspace with Flutter 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) {
if (value?.length === 0) return 'You need to provide one';
}
}) as string;

useNxWrapper ??= await confirm({
message: 'Would you like to use 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? [ https://nx.app/?utm_source=create-nx-flutter ]',
initialValue: false
}) as boolean;

const presetVersion = 'latest';

let directory: string;

const notes = [
'- Go to [ https://github.com/tinesoft/nxrocks/tree/develop/packages/nx-flutter ] to get started with Nx and Flutter plugin.',
`- Run '${getNxCommand(useNxWrapper)} g @nxrocks/nx-flutter:project' to later add additional projects into this workspace.`
];

if (useNxWrapper) {
notes.push('- Go to [ 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, '@nxrocks/nx-flutter', allArgs, useNxCloud, presetVersion, !verbose);

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

notes.push('- Go to [ 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! 🚀`);
}

main();
10 changes: 10 additions & 0 deletions packages/create-nx-flutter/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'create-nx-flutter',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/create-nx-flutter',
};
38 changes: 38 additions & 0 deletions packages/create-nx-flutter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "create-nx-flutter",
"version": "0.0.1",
"license": "MIT",
"author": "Tine Kondo ",
"repository": {
"type": "git",
"url": "https://github.com/tinesoft/nxrocks.git",
"directory": "packages/common"
},
"homepage": "https://github.com/tinesoft/nxrocks/blob/master/packages/create-nx-flutter#readme",
"bugs": {
"url": "https://github.com/tinesoft/nxrocks/issues"
},
"keywords": [
"nx-flutter",
"flutter",
"nx",
"cli"
],
"private": false,
"publishConfig": {
"access": "public"
},
"dependencies": {
"create-nx-workspace": "16.9.0",
"@clack/prompts": "^0.7.0",
"yargs": "^17.7.2",
"@nxrocks/common": "*",
"tslib": "^2.6.1"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"bin": {
"create-nx-flutter": "./bin/index.js"
}
}
49 changes: 49 additions & 0 deletions packages/create-nx-flutter/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "create-nx-flutter",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/create-nx-flutter/bin",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/create-nx-flutter",
"main": "packages/create-nx-flutter/bin/index.ts",
"tsConfig": "packages/create-nx-flutter/tsconfig.lib.json",
"assets": ["packages/create-nx-flutter/*.md"],
"updateBuildableProjectDepsInPackageJson": false
}
},
"publish": {
"command": "node tools/scripts/publish.mjs create-nx-flutter {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/create-nx-flutter/**/*.ts",
"packages/create-nx-flutter/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/create-nx-flutter/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": [],
"implicitDependencies": ["nx-flutter"]
}
16 changes: 16 additions & 0 deletions packages/create-nx-flutter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions packages/create-nx-flutter/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts", "bin/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions packages/create-nx-flutter/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
6 changes: 6 additions & 0 deletions packages/nx-flutter/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"schema": "./src/generators/project/schema.json",
"description": "Generator to generate the project",
"aliases": ["proj", "new", "gen", "init", "create", "generate"]
},
"preset": {
"factory": "./src/generators/preset/generator",
"schema": "./src/generators/preset/schema.json",
"description": "preset generator",
"x-use-standalone-layout": true
}
}
}
Loading

0 comments on commit 5126425

Please sign in to comment.