Skip to content

Commit dc8b897

Browse files
committed
feat(cli, core): write some initial files for demo
- write some initial files for demo - fix in-memory code loader name
1 parent 31f69be commit dc8b897

File tree

11 files changed

+153
-6
lines changed

11 files changed

+153
-6
lines changed

packages/build/tsconfig.lib.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
"declaration": true,
66
"types": []
77
},
8-
"include": ["**/*.ts", "../../types/*.d.ts"],
8+
"include": [
9+
"jest.config.ts",
10+
"**/*.test.ts",
11+
"**/*.spec.ts",
12+
"**/*.d.ts",
13+
"../../types/*.d.ts"
14+
],
915
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"]
1016
}

packages/cli/package.json

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
{
22
"name": "@vulcan-sql/cli",
33
"version": "0.1.0",
4-
"type": "commonjs"
4+
"type": "commonjs",
5+
"bin": {
6+
"vulcan": "./src/index.js"
7+
},
8+
"publishConfig": {
9+
"access": "public"
10+
},
11+
"keywords": [
12+
"vulcan",
13+
"vulcan-sql",
14+
"data",
15+
"sql",
16+
"database",
17+
"data-warehouse",
18+
"data-lake",
19+
"api-builder"
20+
],
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/Canner/vulcan.git"
24+
},
25+
"license": "MIT"
526
}

packages/cli/project.json

+41-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,52 @@
4444
"executor": "@nrwl/workspace:run-commands",
4545
"options": {
4646
"command": "node src/index.js {args.cmd}",
47-
"cwd": "dist/packages/cli"
47+
"cwd": "dist/packages/cli",
48+
"forwardAllArgs": false
4849
},
4950
"dependsOn": [
5051
{
5152
"projects": "self",
5253
"target": "build"
54+
},
55+
{
56+
"projects": "self",
57+
"target": "cp-assets"
58+
}
59+
]
60+
},
61+
"cp-assets": {
62+
"executor": "@nrwl/workspace:run-commands",
63+
"options": {
64+
"commands": [
65+
{
66+
"command": "cp -r packages/cli/src/schemas dist/packages/cli/src",
67+
"forwardAllArgs": false
68+
}
69+
]
70+
},
71+
"dependsOn": [
72+
{
73+
"projects": "self",
74+
"target": "build"
75+
}
76+
]
77+
},
78+
"install": {
79+
"executor": "@nrwl/workspace:run-commands",
80+
"options": {
81+
"commands": [
82+
{
83+
"command": "chmod +x ./src/index.js && npm i -g . && echo done.",
84+
"forwardAllArgs": false
85+
}
86+
],
87+
"cwd": "dist/packages/cli"
88+
},
89+
"dependsOn": [
90+
{
91+
"projects": "self",
92+
"target": "cp-assets"
5393
}
5494
]
5595
}

packages/cli/src/commands/init.ts

+42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as path from 'path';
55
import { version } from '../../package.json';
66
import * as ora from 'ora';
77
import { exec } from 'child_process';
8+
import * as nunjucks from 'nunjucks';
9+
import * as glob from 'glob';
810

911
const validators: Record<
1012
string,
@@ -73,6 +75,7 @@ export class InitCommand extends Command {
7375
name: options.projectName,
7476
dependencies: {
7577
'@vulcan-sql/core': options.version,
78+
// TODO: Install build/serve package when they are ready
7679
},
7780
},
7881
null,
@@ -84,6 +87,13 @@ export class InitCommand extends Command {
8487
installSpinner.start('Installing dependencies...');
8588
await this.execAndWait(`yarn --silent`, projectPath);
8689
installSpinner.succeed(`Dependencies have been installed.`);
90+
installSpinner.start('Writing initial content...');
91+
await this.addInitFiles(projectPath, options);
92+
installSpinner.succeed('Initial done.');
93+
94+
this.logger.info(
95+
`Project has been initialized. Run "cd ${projectPath} && vulcan start" to start the server.`
96+
);
8797
} catch (e) {
8898
installSpinner.fail();
8999
throw e;
@@ -102,4 +112,36 @@ export class InitCommand extends Command {
102112
});
103113
});
104114
}
115+
116+
private async addInitFiles(projectPath: string, options: InitCommandOptions) {
117+
const files = await this.listFiles(
118+
path.resolve(__dirname, '..', 'schemas', 'init', '**/*.*')
119+
);
120+
for (const file of files) {
121+
const relativePath = path.relative(
122+
path.resolve(__dirname, '..', 'schemas', 'init'),
123+
file
124+
);
125+
let templateContent = await fs.readFile(file, 'utf8');
126+
if (path.extname(file) === '.yaml') {
127+
// Only render yaml files because sql files have some template scripts which are used by Vulcan.
128+
templateContent = nunjucks.renderString(templateContent, {
129+
options,
130+
});
131+
}
132+
const targetPath = path.resolve(projectPath, relativePath);
133+
const dir = path.dirname(targetPath);
134+
await fs.mkdir(dir, { recursive: true });
135+
await fs.writeFile(targetPath, templateContent, 'utf8');
136+
}
137+
}
138+
139+
private async listFiles(path: string) {
140+
return new Promise<string[]>((resolve, reject) => {
141+
glob(path, (err, files) => {
142+
if (err) return reject(err);
143+
return resolve(files);
144+
});
145+
});
146+
}
105147
}

packages/cli/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env node
2+
13
import { program } from 'commander';
24
import { InitCommand } from './commands';
35
import { Logger } from 'tslog';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
select *
2+
from public.users
3+
where id = "{{ context.params.id }}"
4+
limit 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
url: /user/:id
2+
request:
3+
- fieldName: id
4+
fieldIn: path
5+
description: user id
6+
validators:
7+
- uuid
8+
- required
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: '{{ options.projectName }}'
2+
description: A starter Vulcan project
3+
version: 0.1.0
4+
template:
5+
provider: LocalFile
6+
# Path to .sql files
7+
folderPath: sqls
8+
codeLoader: InMemory
9+
artifact:
10+
provider: LocalFile
11+
serializer: JSON
12+
# Path to build result
13+
filePath: result.json
14+
schema-parser:
15+
reader: LocalFile
16+
# Path to .yaml files
17+
folderPath: sqls
18+
document-generator:
19+
spec:
20+
- oas3
21+
folderPath: .
22+
types:
23+
- RESTFUL

packages/cli/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"noPropertyAccessFromIndexSignature": true,
99
"noImplicitReturns": true,
1010
"noFallthroughCasesInSwitch": true,
11-
"resolveJsonModule": true
11+
"resolveJsonModule": true,
12+
"sourceMap": false
1213
},
1314
"files": [],
1415
"include": [],

packages/cli/tsconfig.lib.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"declaration": true,
66
"types": ["node"]
77
},
8-
"include": ["**/*.ts"],
8+
"include": ["**/*.ts", "../../types/*.d.ts"],
99
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"]
1010
}

packages/core/src/lib/template-engine/code-loader/inMemoryCodeLoader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@vulcan-sql/core/models';
77

88
@VulcanInternalExtension()
9-
@VulcanExtensionId('inMemory')
9+
@VulcanExtensionId('InMemory')
1010
export class InMemoryCodeLoader extends CodeLoader {
1111
private source = new Map<string, object>();
1212

0 commit comments

Comments
 (0)