Skip to content

Commit

Permalink
feat(sdk-schematics): initialize git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Feb 19, 2024
1 parent 3f482f9 commit 9651d8b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 9 deletions.
11 changes: 11 additions & 0 deletions packages/@ama-sdk/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
prepareTestEnv,
setupLocalRegistry
} from '@o3r/test-helpers';
import { execFileSync } from 'node:child_process';
import * as fs from 'node:fs';
import { cpSync, mkdirSync } from 'node:fs';
import * as path from 'node:path';
Expand Down Expand Up @@ -52,11 +53,21 @@ describe('Create new sdk command', () => {
test('should generate a full SDK when the specification is provided', () => {
expect(() =>
packageManagerCreate(`@ama-sdk typescript ${sdkPackageName} --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(true);
expect(() => execFileSync('git', ['diff', '--quiet', '--exit-code'], { cwd: sdkPackagePath })).not.toThrow(); // git must be ready to commit all files
expect(() => packageManagerRun('build', { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
});

test('should not generate a git repository when using --skip-git', () => {
expect(() =>
packageManagerCreate(`@ama-sdk typescript ${sdkPackageName} --skip-git --package-manager ${packageManager} --spec-path ./swagger-spec.yml`, execAppOptions)).not.toThrow();
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(false);
});

test('should generate an empty SDK ready to be used', () => {
expect(() => packageManagerCreate(`@ama-sdk typescript ${sdkPackageName}`, execAppOptions)).not.toThrow();
expect(fs.existsSync(path.join(sdkPackagePath, '.git'))).toBe(true);
expect(() => execFileSync('git', ['diff', '--quiet', '--exit-code'], { cwd: sdkPackagePath })).not.toThrow(); // git must be ready to commit all files
expect(() => packageManagerRun('build', { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
expect(() =>
packageManagerExec(
Expand Down
15 changes: 11 additions & 4 deletions packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const packageManagerEnv = process.env.npm_config_user_agent?.split('/')[0];
const defaultScope = 'sdk';
const binPath = resolve(require.resolve('@angular-devkit/schematics-cli/package.json'), '../bin/schematics.js');
const args = process.argv.slice(2);
const argv = minimist(args);
const argv = minimist(args, {
'boolean': ['skip-git']
});

let defaultPackageManager = 'npm';
if (packageManagerEnv && ['npm', 'yarn'].includes(packageManagerEnv)) {
Expand Down Expand Up @@ -64,13 +66,16 @@ const getYarnVersion = () => {
}
};

const skipGit = !!argv['skip-git'];

const schematicArgs = [
argv.debug !== undefined ? `--debug=${argv.debug as string}` : '--debug=false', // schematics enable debug mode per default when using schematics with relative path
'--name', name,
'--package', pck,
'--package-manager', packageManager,
'--directory', targetDirectory,
...(argv['spec-path'] ? ['--spec-path', argv['spec-path']] : [])
...(argv['spec-path'] ? ['--spec-path', argv['spec-path']] : []),
...(skipGit ? ['--skip-git'] : [])
];

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

const run = () => {

const cwd = resolve(process.cwd(), targetDirectory);
const steps: { args: string[]; cwd?: string; runner?: string }[] = [
getSchematicStepInfo(schematicsToRun[0]),
...(
packageManager === 'yarn'
? [{ runner: 'yarn', args: ['set', 'version', getYarnVersion()], cwd: resolve(process.cwd(), targetDirectory)}]
? [{ runner: 'yarn', args: ['set', 'version', getYarnVersion()], cwd}]
: []
),
...schematicsToRun.slice(1).map(getSchematicStepInfo)
...schematicsToRun.slice(1).map(getSchematicStepInfo),
...(skipGit ? [] : [{ runner: 'git', args: ['add', '.'], cwd}])
];

const errors = steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ describe('Typescript Shell Generator', () => {
yarnTree = await runner.runSchematic('typescript-shell', {
name: 'test-scope',
package: 'test-sdk',
skipInstall: true
skipInstall: true,
skipGit: true
}, Tree.empty());
npmTree = await runner.runSchematic('typescript-shell', {
name: 'test-scope',
package: 'test-sdk',
skipInstall: true,
skipGit: true,
packageManager: 'npm'
}, Tree.empty());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import {RepositoryInitializerTask} from '@angular-devkit/schematics/tasks';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import {dump, load} from 'js-yaml';
import {isAbsolute, posix, relative} from 'node:path';
Expand All @@ -29,6 +30,13 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati
context.addTask(installTask);
};

const initGitRule = (_tree: Tree, context: SchematicContext) => {
const workingDirectory = options.directory ? (isAbsolute(options.directory) ? relative(process.cwd(), options.directory) : options.directory) : '.';
const defaultCommitOptions = { message: 'Initial commit' };
const commitOptions = options.commit === true ? defaultCommitOptions : typeof options.commit === 'object' ? {...defaultCommitOptions, ...options.commit} : undefined;
context.addTask(new RepositoryInitializerTask(workingDirectory, commitOptions));
};

const setupRule = async (tree: Tree, context: SchematicContext) => {
const amaSdkSchematicsPackageJson = await readPackageJson();

Expand Down Expand Up @@ -108,7 +116,8 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati

return chain([
setupRule,
...(options.skipInstall ? [] : [installRule])
...(options.skipInstall ? [] : [installRule]),
...(options.skipGit ? [] : [initGitRule])
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@
"description": "Skip NPM install",
"default": false
},
"commit": {
"description": "Initial git repository commit information.",
"oneOf": [
{
"type": "boolean"
},
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"message": {
"type": "string"
}
}
}
]
},
"skipGit": {
"description": "Do not initialize a git repository.",
"type": "boolean",
"default": false
},
"packageManager": {
"type": "string",
"enum": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ export interface NgGenerateTypescriptSDKShellSchematicsSchema extends SchematicO

/** Skip NPM install */
skipInstall: boolean;

/** Initial git repository commit information. */
commit: boolean | { name?: string; email?: string; message?: string };

/** Do not initialize a git repository. */
skipGit: boolean;
}
6 changes: 3 additions & 3 deletions packages/@o3r/test-helpers/src/prepare-test-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ export async function prepareTestEnv(folderName: string, type: PrepareTestEnvTyp
const baseAppPath = path.join(itTestsFolderPath, baseApp);
cpSync(baseAppPath, appFolderPath, {recursive: true, dereference: true, filter: (source) => !/node_modules/.test(source)});
packageManagerInstall(execAppOptions);
}

// Setup git and initial commit to easily make checks on the diff inside the tests
setupGit(appFolderPath);
// Setup git and initial commit to easily make checks on the diff inside the tests
setupGit(appFolderPath);
}

return appFolderPath;
}

0 comments on commit 9651d8b

Please sign in to comment.