Skip to content

Commit

Permalink
Update init command to allow specifying spec version
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Sep 28, 2021
1 parent aaacd38 commit dabf3b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
26 changes: 20 additions & 6 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path';
import {Command, flags} from '@oclif/command';
import cli from 'cli-ux';
import {createProject} from '../controller/init-controller';
import {ProjectSpec} from '../types';
import {ProjectSpecBase, ProjectSpecV0_0_1, ProjectSpecV0_2_0} from '../types';

export default class Init extends Command {
static description = 'Init a scaffold subquery project';
Expand All @@ -16,6 +16,11 @@ export default class Init extends Command {
starter: flags.boolean({
default: true,
}),
specVersion: flags.string({
required: false,
default: '0.0.1',
description: 'The spec version to be used by the project',
}),
};

static args = [
Expand All @@ -27,7 +32,7 @@ export default class Init extends Command {

async run(): Promise<void> {
const {args, flags} = this.parse(Init);
const project = {} as ProjectSpec;
const project = {} as ProjectSpecBase;

project.name = args.projectName
? args.projectName
Expand All @@ -36,10 +41,19 @@ export default class Init extends Command {
throw new Error(`Directory ${project.name} exists, try another project name`);
}
project.repository = await cli.prompt('Git repository', {required: false});
project.endpoint = await cli.prompt('RPC endpoint', {
default: 'wss://polkadot.api.onfinality.io/public-ws',
required: true,
});

if (flags.specVersion === '0.2.0') {
(project as ProjectSpecV0_2_0).genesisHash = await cli.prompt('Network genesis hash', {
default: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', // Polkadot
required: true,
});
} else {
(project as ProjectSpecV0_0_1).endpoint = await cli.prompt('RPC endpoint', {
default: 'wss://polkadot.api.onfinality.io/public-ws',
required: true,
});
}

project.author = await cli.prompt('Authors', {required: true});
project.description = await cli.prompt('Description', {required: false});
project.version = await cli.prompt('Version:', {default: '1.0.0', required: true});
Expand Down
26 changes: 18 additions & 8 deletions packages/cli/src/controller/init-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import fs from 'fs';
import * as path from 'path';
import {promisify} from 'util';
import {ProjectManifestV0_0_1 as ProjectManifest} from '@subql/common';
import {ProjectManifestV0_0_1, ProjectManifestV0_2_0} from '@subql/common';
import yaml from 'js-yaml';
import rimraf from 'rimraf';
import simpleGit from 'simple-git';
import {ProjectSpec} from '../types';
import {isProjectSpecV0_0_1, isProjectSpecV0_2_0, ProjectSpecBase} 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: ProjectSpecBase): Promise<void> {
const projectPath = path.join(localPath, project.name);

const cloneArgs = isProjectSpecV0_2_0(project) ? ['-b', 'v0.2.0', '--single-branch'] : [];
try {
await simpleGit().clone(STARTER_PATH, projectPath /*, ['-b', 'v0.0.2', '--single-branch']*/);
await simpleGit().clone(STARTER_PATH, projectPath, cloneArgs);
} catch (e) {
throw new Error('Failed to clone starter template from git');
}
Expand All @@ -36,7 +38,7 @@ export async function createProject(localPath: string, project: ProjectSpec): Pr
}
}

async function preparePackage(projectPath: string, project: ProjectSpec): Promise<void> {
async function preparePackage(projectPath: string, project: ProjectSpecBase): Promise<void> {
//load and write package.json
const packageData = await fs.promises.readFile(`${projectPath}/package.json`);
const currentPackage = JSON.parse(packageData.toString());
Expand All @@ -49,14 +51,22 @@ async function preparePackage(projectPath: string, project: ProjectSpec): Promis
await fs.promises.writeFile(`${projectPath}/package.json`, newPackage, 'utf8');
}

async function prepareManifest(projectPath: string, project: ProjectSpec): Promise<void> {
async function prepareManifest(projectPath: string, project: ProjectSpecBase): Promise<void> {
//load and write manifest(project.yaml)
const yamlPath = path.join(`${projectPath}`, `project.yaml`);
const manifest = await fs.promises.readFile(yamlPath, 'utf8');
const data = yaml.load(manifest) as ProjectManifest;
const data = yaml.load(manifest) as ProjectManifestV0_0_1 | ProjectManifestV0_2_0;
data.description = project.description ?? data.description;
data.network.endpoint = project.endpoint;
data.repository = project.repository ?? '';

if (isProjectSpecV0_0_1(project)) {
(data as ProjectManifestV0_0_1).network.endpoint = project.endpoint;
} else if (isProjectSpecV0_2_0(project)) {
(data as ProjectManifestV0_2_0).version = project.version;
(data as ProjectManifestV0_2_0).name = project.name;
data.network.genesisHash = project.genesisHash;
}

const newYaml = yaml.dump(data);
await fs.promises.writeFile(yamlPath, newYaml, 'utf8');
}
19 changes: 17 additions & 2 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

export interface ProjectSpec {
export interface ProjectSpecBase {
name: string;
repository?: string;
endpoint: string;
author: string;
description?: string;
version: string;
license: string;
}

export interface ProjectSpecV0_0_1 extends ProjectSpecBase {
endpoint: string;
}

export interface ProjectSpecV0_2_0 extends ProjectSpecBase {
genesisHash: string;
}

export function isProjectSpecV0_0_1(projectSpec: ProjectSpecBase): projectSpec is ProjectSpecV0_0_1 {
return !!(projectSpec as ProjectSpecV0_0_1).endpoint;
}

export function isProjectSpecV0_2_0(projectSpec: ProjectSpecBase): projectSpec is ProjectSpecV0_2_0 {
return !!(projectSpec as ProjectSpecV0_2_0).genesisHash;
}

0 comments on commit dabf3b4

Please sign in to comment.