Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js/cli) remove --no-project flag #606

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions js/cli/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ import { ProjectBuilder } from './generate/index.js';

const program = new Command();

const handler = async (path: string, out: string, name: string, project: boolean, yes: boolean) => {
const handler = async (path: string, out: string, name: string, yes: boolean) => {
const parser = new SailsIdlParser();
await parser.init();
const sails = new Sails(parser);

const projectBuilder = new ProjectBuilder(sails, name)
.setRootPath(out)
.setIdlPath(path)
.setIsProject(project)
.setAutomaticOverride(yes);
const projectBuilder = new ProjectBuilder(sails, name).setRootPath(out).setIdlPath(path).setAutomaticOverride(yes);

await projectBuilder.build();
};

program
.command('generate <path-to-file.sails.idl>')
.option('--no-project', 'Generate single file without project structure')
.option('-n --name <name>', 'Name of the library', 'program')
.option('-o --out <path-to-dir>', 'Output directory')
.option('-y --yes', 'Automatic yes to file override prompts')
Expand All @@ -40,7 +35,7 @@ program
},
) => {
try {
await handler(path, options.out, options.name, options.project, options.yes);
await handler(path, options.out, options.name, options.yes);
} catch (error) {
console.error(error.message);
process.exit(1);
Expand Down
22 changes: 4 additions & 18 deletions js/cli/src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import * as config from '../config.json';

export class ProjectBuilder {
private projectPath = ['.', 'src'];
private isProject: boolean = true;
private isAutomaricOverride: boolean = false;

constructor(
Expand Down Expand Up @@ -62,12 +61,6 @@ export class ProjectBuilder {
return this;
}

setIsProject(isProject: boolean) {
this.isProject = isProject;

return this;
}

setAutomaticOverride(isAutomaricOverride: boolean) {
this.isAutomaricOverride = isAutomaricOverride;

Expand All @@ -78,33 +71,26 @@ export class ProjectBuilder {
const rootPath = this.projectPath[0];
const srcPath = path.join(...this.projectPath);

const libPath = this.isProject ? srcPath : rootPath;

if (!existsSync(libPath)) {
mkdirSync(libPath, { recursive: true });
if (!existsSync(srcPath)) {
mkdirSync(srcPath, { recursive: true });
}

const libCode = this.generateLib();
const libFile = path.join(libPath, 'lib.ts');
const libFile = path.join(srcPath, 'lib.ts');
if (await this.canCreateFile(libFile)) {
writeFileSync(libFile, libCode);
} else {
process.exit(0);
}

const typesCode = this.generateTypes();
const typesFile = path.join(libPath, 'global.d.ts');
const typesFile = path.join(srcPath, 'global.d.ts');
if (await this.canCreateFile(typesFile)) {
writeFileSync(typesFile, typesCode);
} else {
process.exit(0);
}

if (!this.isProject) {
console.log(`Lib generated at ${libPath}`);
return;
}

const tsconfigPath = path.join(rootPath, 'tsconfig.json');
const pkgJsonPath = path.join(rootPath, 'package.json');

Expand Down
2 changes: 1 addition & 1 deletion js/test/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hexToU8a } from '@polkadot/util';
import { readFileSync } from 'fs';

import { getFnNamePrefix, getServiceNamePrefix, H256, NonZeroU32, NonZeroU8, Sails, ZERO_ADDRESS } from '..';
import { Program } from './demo/lib';
import { Program } from './demo/src/lib';
import { SailsIdlParser } from 'sails-js-parser';

let sails: Sails;
Expand Down
6 changes: 3 additions & 3 deletions js/test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export default () => {
execSync('rm -rf ./test/demo');

// Generate demo ts client
execSync('node cli/build/app.js generate ../examples/demo/client/demo.idl -o ./test/demo --no-project --yes');
execSync('node cli/build/app.js generate ../examples/demo/client/demo.idl -o ./test/demo --yes');

// Modify client imports

const filesToModify = ['test/demo/lib.ts'];
const filesToModify = ['test/demo/src/lib.ts'];

for (const path of filesToModify) {
const data = fs.readFileSync(path, 'utf8').replace(`from 'sails-js'`, `from '../..'`);
const data = fs.readFileSync(path, 'utf8').replace(`from 'sails-js'`, `from '../../..'`);
fs.writeFileSync(path, data, 'utf8');
}
};