Skip to content

Commit

Permalink
Expand on cli init capabilities (#485)
Browse files Browse the repository at this point in the history
* Expand on cli `init` capabilities

- Install dependencies by default, can be skipped through `--skip-install`
- Run codegen when initialising a project, can be skipped through `--skip-codegen`
- Add the ability to specify a directory, same applies to `codegen` command

* Remove codegen from init command

* Default to not installing dependencies

* Update README.md
  • Loading branch information
stwiname authored Sep 29, 2021
1 parent 11e81e1 commit 53ecf80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ npm install -g @subql/cli
$ subql COMMAND
running command...
$ subql (-v|--version|version)
@subql/cli/0.12.1-0 linux-x64 node-v14.17.6
@subql/cli/0.12.1-0 darwin-x64 node-v14.15.1
$ subql --help [COMMAND]
USAGE
$ subql COMMAND
Expand Down Expand Up @@ -63,6 +63,7 @@ USAGE
OPTIONS
-f, --force
-l, --location=location local folder to run codegen in
--file=file
```

Expand Down Expand Up @@ -98,6 +99,9 @@ ARGUMENTS
OPTIONS
-f, --force
-l, --location=location local folder to create the project in
--install-dependencies Install dependencies as well
--npm Force using NPM instead of yarn, only works with `install-dependencies` flag
--starter
```

Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import path from 'path';
import {Command, flags} from '@oclif/command';
import {codegen} from '../controller/codegen-controller';

Expand All @@ -10,14 +11,18 @@ export default class Codegen extends Command {
static flags = {
force: flags.boolean({char: 'f'}),
file: flags.string(),
location: flags.string({char: 'l', description: 'local folder to run codegen in'}),
};

async run(): Promise<void> {
const {flags} = this.parse(Codegen);
this.log('===============================');
this.log('---------Subql Codegen---------');
this.log('===============================');

const location = flags.location ? path.resolve(flags.location) : process.cwd();
try {
await codegen(process.cwd());
await codegen(location);
} catch (err) {
console.error(err.message);
process.exit(1);
Expand Down
24 changes: 18 additions & 6 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from 'fs';
import path from 'path';
import {Command, flags} from '@oclif/command';
import cli from 'cli-ux';
import {createProject} from '../controller/init-controller';
import {createProject, installDependencies} from '../controller/init-controller';
import {ProjectSpec} from '../types';

export default class Init extends Command {
Expand All @@ -16,6 +16,9 @@ export default class Init extends Command {
starter: flags.boolean({
default: true,
}),
location: flags.string({char: 'l', description: 'local folder to create the project in'}),
'install-dependencies': flags.boolean({description: 'Install dependencies as well', default: false}),
npm: flags.boolean({description: 'Force using NPM instead of yarn, only works with `install-dependencies` flag'}),
};

static args = [
Expand All @@ -29,10 +32,12 @@ export default class Init extends Command {
const {args, flags} = this.parse(Init);
const project = {} as ProjectSpec;

const location = flags.location ? path.resolve(flags.location) : process.cwd();

project.name = args.projectName
? args.projectName
: await cli.prompt('Project name', {default: 'subql-starter', required: true});
if (fs.existsSync(path.join(process.cwd(), `${project.name}`))) {
if (fs.existsSync(path.join(location, `${project.name}`))) {
throw new Error(`Directory ${project.name} exists, try another project name`);
}
project.repository = await cli.prompt('Git repository', {required: false});
Expand All @@ -48,12 +53,19 @@ export default class Init extends Command {
if (flags.starter && project.name) {
cli.action.start('Init the starter package');
try {
await createProject(process.cwd(), project);
cli.action.stop(`${project.name} is ready`);
const projectPath = await createProject(location, project);
cli.action.stop();

if (flags['install-dependencies']) {
cli.action.start('Installing dependencies');
installDependencies(projectPath, flags.npm);
cli.action.stop();
}

this.log(`${project.name} is ready`);
} catch (e) {
/* handle all errors here */
console.error(e.message);
process.exit(1);
this.error(e.message);
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion packages/cli/src/controller/init-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import childProcess from 'child_process';
import fs from 'fs';
import * as path from 'path';
import {promisify} from 'util';
Expand All @@ -12,7 +13,7 @@ import {ProjectSpec} from '../types';

const STARTER_PATH = 'https://github.com/subquery/subql-starter';

export async function createProject(localPath: string, project: ProjectSpec): Promise<void> {
export async function createProject(localPath: string, project: ProjectSpec): Promise<string> {
const projectPath = path.join(localPath, project.name);
try {
await simpleGit().clone(STARTER_PATH, projectPath);
Expand All @@ -34,6 +35,8 @@ export async function createProject(localPath: string, project: ProjectSpec): Pr
} catch (e) {
throw new Error('Failed to remove .git from starter project');
}

return projectPath;
}

async function preparePackage(projectPath: string, project: ProjectSpec): Promise<void> {
Expand All @@ -60,3 +63,22 @@ async function prepareManifest(projectPath: string, project: ProjectSpec): Promi
const newYaml = yaml.dump(data);
await fs.promises.writeFile(yamlPath, newYaml, 'utf8');
}

export function installDependencies(projectPath: string, useNpm?: boolean): void {
let command = 'yarn install';

if (useNpm || !checkYarnExists()) {
command = 'npm install';
}

childProcess.execSync(command, {cwd: projectPath});
}

function checkYarnExists(): boolean {
try {
childProcess.execSync('yarn --version');
return true;
} catch (e) {
return false;
}
}

0 comments on commit 53ecf80

Please sign in to comment.