Skip to content

Commit 5066f28

Browse files
committed
feat(sdk-schematics): initialize git repository
1 parent 3f482f9 commit 5066f28

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

packages/@ama-sdk/create/src/index.it.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
prepareTestEnv,
1010
setupLocalRegistry
1111
} from '@o3r/test-helpers';
12+
import { execFileSync } from 'node:child_process';
1213
import * as fs from 'node:fs';
1314
import { cpSync, mkdirSync } from 'node:fs';
1415
import * as path from 'node:path';
@@ -52,11 +53,21 @@ describe('Create new sdk command', () => {
5253
test('should generate a full SDK when the specification is provided', () => {
5354
expect(() =>
5455
packageManagerCreate(`@ama-sdk typescript ${sdkPackageName} --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
56+
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(true);
57+
expect(() => execFileSync('git', ['diff', '--quiet', '--exit-code'], { cwd: sdkPackagePath })).not.toThrow(); // git must be ready to commit all files
5558
expect(() => packageManagerRun('build', { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
5659
});
5760

61+
test('should not generate a git repository when using --skip-git', () => {
62+
expect(() =>
63+
packageManagerCreate(`@ama-sdk typescript ${sdkPackageName} --skip-git --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
64+
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(false);
65+
});
66+
5867
test('should generate an empty SDK ready to be used', () => {
5968
expect(() => packageManagerCreate(`@ama-sdk typescript ${sdkPackageName}`, execAppOptions)).not.toThrow();
69+
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(true);
70+
expect(() => execFileSync('git', ['diff', '--quiet', '--exit-code'], { cwd: sdkPackagePath })).not.toThrow(); // git must be ready to commit all files
6071
expect(() => packageManagerRun('build', { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
6172
expect(() =>
6273
packageManagerExec(

packages/@ama-sdk/create/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const packageManagerEnv = process.env.npm_config_user_agent?.split('/')[0];
99
const defaultScope = 'sdk';
1010
const binPath = resolve(require.resolve('@angular-devkit/schematics-cli/package.json'), '../bin/schematics.js');
1111
const args = process.argv.slice(2);
12-
const argv = minimist(args);
12+
const argv = minimist(args, {
13+
'boolean': ['skip-git']
14+
});
1315

1416
let defaultPackageManager = 'npm';
1517
if (packageManagerEnv && ['npm', 'yarn'].includes(packageManagerEnv)) {
@@ -64,13 +66,16 @@ const getYarnVersion = () => {
6466
}
6567
};
6668

69+
const skipGit = !!argv['skip-git'];
70+
6771
const schematicArgs = [
6872
argv.debug !== undefined ? `--debug=${argv.debug as string}` : '--debug=false', // schematics enable debug mode per default when using schematics with relative path
6973
'--name', name,
7074
'--package', pck,
7175
'--package-manager', packageManager,
7276
'--directory', targetDirectory,
73-
...(argv['spec-path'] ? ['--spec-path', argv['spec-path']] : [])
77+
...(argv['spec-path'] ? ['--spec-path', argv['spec-path']] : []),
78+
...(skipGit ? ['--skip-git'] : [])
7479
];
7580

7681
const getSchematicStepInfo = (schematic: string) => ({
@@ -79,14 +84,16 @@ const getSchematicStepInfo = (schematic: string) => ({
7984

8085
const run = () => {
8186

87+
const cwd = resolve(process.cwd(), targetDirectory);
8288
const steps: { args: string[]; cwd?: string; runner?: string }[] = [
8389
getSchematicStepInfo(schematicsToRun[0]),
8490
...(
8591
packageManager === 'yarn'
86-
? [{ runner: 'yarn', args: ['set', 'version', getYarnVersion()], cwd: resolve(process.cwd(), targetDirectory)}]
92+
? [{ runner: 'yarn', args: ['set', 'version', getYarnVersion()], cwd}]
8793
: []
8894
),
89-
...schematicsToRun.slice(1).map(getSchematicStepInfo)
95+
...schematicsToRun.slice(1).map(getSchematicStepInfo),
96+
...(skipGit ? [] : [{ runner: 'git', args: ['add', '.'], cwd}])
9097
];
9198

9299
const errors = steps

packages/@ama-sdk/schematics/schematics/typescript/shell/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Tree,
1111
url
1212
} from '@angular-devkit/schematics';
13+
import {RepositoryInitializerTask} from '@angular-devkit/schematics/tasks';
1314
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
1415
import {dump, load} from 'js-yaml';
1516
import {isAbsolute, posix, relative} from 'node:path';
@@ -29,6 +30,13 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati
2930
context.addTask(installTask);
3031
};
3132

33+
const initGitRule = (_tree: Tree, context: SchematicContext) => {
34+
const workingDirectory = options.directory ? (isAbsolute(options.directory) ? relative(process.cwd(), options.directory) : options.directory) : '.';
35+
const defaultCommitOptions = { message: 'Initial commit' };
36+
const commitOptions = options.commit === true ? defaultCommitOptions : typeof options.commit === 'object' ? {...defaultCommitOptions, ...options.commit} : undefined;
37+
context.addTask(new RepositoryInitializerTask(workingDirectory, commitOptions));
38+
};
39+
3240
const setupRule = async (tree: Tree, context: SchematicContext) => {
3341
const amaSdkSchematicsPackageJson = await readPackageJson();
3442

@@ -108,7 +116,8 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati
108116

109117
return chain([
110118
setupRule,
111-
...(options.skipInstall ? [] : [installRule])
119+
...(options.skipInstall ? [] : [installRule]),
120+
...(options.skipGit ? [] : [initGitRule])
112121
]);
113122
}
114123

packages/@ama-sdk/schematics/schematics/typescript/shell/schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,34 @@
2828
"description": "Skip NPM install",
2929
"default": false
3030
},
31+
"commit": {
32+
"description": "Initial git repository commit information.",
33+
"oneOf": [
34+
{
35+
"type": "boolean"
36+
},
37+
{
38+
"type": "object",
39+
"properties": {
40+
"name": {
41+
"type": "string"
42+
},
43+
"email": {
44+
"type": "string",
45+
"format": "email"
46+
},
47+
"message": {
48+
"type": "string"
49+
}
50+
}
51+
}
52+
]
53+
},
54+
"skipGit": {
55+
"description": "Do not initialize a git repository.",
56+
"type": "boolean",
57+
"default": false
58+
},
3159
"packageManager": {
3260
"type": "string",
3361
"enum": [

packages/@ama-sdk/schematics/schematics/typescript/shell/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ export interface NgGenerateTypescriptSDKShellSchematicsSchema extends SchematicO
1818

1919
/** Skip NPM install */
2020
skipInstall: boolean;
21+
22+
/** Initial git repository commit information. */
23+
commit: boolean | { name?: string; email?: string; message?: string };
24+
25+
/** Do not initialize a git repository. */
26+
skipGit: boolean;
2127
}

packages/@o3r/test-helpers/src/prepare-test-env.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ export async function prepareTestEnv(folderName: string, type: PrepareTestEnvTyp
131131
const baseAppPath = path.join(itTestsFolderPath, baseApp);
132132
cpSync(baseAppPath, appFolderPath, {recursive: true, dereference: true, filter: (source) => !/node_modules/.test(source)});
133133
packageManagerInstall(execAppOptions);
134-
}
135134

136-
// Setup git and initial commit to easily make checks on the diff inside the tests
137-
setupGit(appFolderPath);
135+
// Setup git and initial commit to easily make checks on the diff inside the tests
136+
setupGit(appFolderPath);
137+
}
138138

139139
return appFolderPath;
140140
}

0 commit comments

Comments
 (0)