Skip to content

Commit

Permalink
feat: add generators internal options
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangsibecas committed Sep 21, 2024
1 parent 52a2573 commit a5bda86
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/generators/application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { initGenerator } from '../init/generator';

export async function applicationGenerator(
tree: Tree,
options: ApplicationGeneratorSchema,
schema: ApplicationGeneratorSchema
) {
await initGenerator(tree);
await initGenerator(tree, schema);

let projectName = options.name;
let projectRoot = options.directory;
let projectName = schema.name;
let projectRoot = schema.directory;

const nameAndRoot = await determineProjectNameAndRootOptions(tree, {
...options,
projectNameAndRootFormat: options.projectNameAndRootFormat,
...schema,
projectNameAndRootFormat: schema.projectNameAndRootFormat,
projectType: 'application',
callingGenerator: 'nx-foundry:application',
});
Expand All @@ -37,7 +37,7 @@ export async function applicationGenerator(
});

generateFiles(tree, path.join(__dirname, 'files'), projectRoot, {
...options,
...schema,
root: projectRoot,
});

Expand Down
3 changes: 3 additions & 0 deletions src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export interface ApplicationGeneratorSchema {
name: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
directory?: string;
skipPackageJson?: boolean;
skipFormat?: boolean;
keepExistingVersions?: boolean;
}
21 changes: 20 additions & 1 deletion src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,26 @@
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`.",
"x-priority": "internal"
},
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
}
},
"required": ["name"]
"required": [
"name"
]
}
10 changes: 5 additions & 5 deletions src/generators/init/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ jobs:
with:
version: 8
`,
`
);
});

it('should add plugin to nx.json', async () => {
await initGenerator(tree);
await initGenerator(tree, {});
const nxJson = readNxJson(tree);

expect(nxJson.plugins).toMatchObject([
Expand All @@ -59,16 +59,16 @@ jobs:
});

it('should add .gitignore entry', async () => {
await initGenerator(tree);
await initGenerator(tree, {});
const gitIgnore = tree.read('.gitignore', 'utf-8');

expect(gitIgnore.includes('# Foundry')).toBe(true);
});

it('should add github actions step', async () => {
await initGenerator(tree);
await initGenerator(tree, {});
expect(tree.read('.github/workflows/ci.yml', 'utf8')).toContain(
'Install Foundry',
'Install Foundry'
);
});
});
46 changes: 41 additions & 5 deletions src/generators/init/generator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import { formatFiles, readNxJson, Tree, updateNxJson } from '@nx/devkit';
import { addGithubActionCI, addGitIgnoreEntry } from '../../utils';
import {
addDependenciesToPackageJson,
formatFiles,
readNxJson,
removeDependenciesFromPackageJson,
Tree,
updateNxJson,
} from '@nx/devkit';
import {
addGithubActionCI,
addGitIgnoreEntry,
nxFoundryVersion,
} from '../../utils';
import { InitGeneratorSchema } from './schema';

export async function initGenerator(tree: Tree) {
const updateDependencies = (tree: Tree, schema: InitGeneratorSchema) => {
removeDependenciesFromPackageJson(tree, ['nx-foundry'], []);

return addDependenciesToPackageJson(
tree,
{},
{
'nx-foundry': nxFoundryVersion,
},
undefined,
schema.keepExistingVersions
);
};

const setupTargets = (tree: Tree) => {
const nxJson = readNxJson(tree) || {};

const hasPlugin = nxJson.plugins?.some((p) =>
typeof p === 'string' ? p === 'nx-foundry' : p.plugin === 'nx-foundry',
typeof p === 'string' ? p === 'nx-foundry' : p.plugin === 'nx-foundry'
);

if (!hasPlugin) {
Expand All @@ -29,11 +55,21 @@ export async function initGenerator(tree: Tree) {
}

updateNxJson(tree, nxJson);
};

export async function initGenerator(tree: Tree, schema: InitGeneratorSchema) {
setupTargets(tree);

addGitIgnoreEntry(tree);
addGithubActionCI(tree);

await formatFiles(tree);
if (!schema.skipFormat) {
await formatFiles(tree);
}

return !schema.skipPackageJson
? updateDependencies(tree, schema)
: () => null;
}

export default initGenerator;
5 changes: 5 additions & 0 deletions src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface InitGeneratorSchema {
skipPackageJson?: boolean;
skipFormat?: boolean;
keepExistingVersions?: boolean;
}
21 changes: 19 additions & 2 deletions src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
"$id": "Init",
"title": "",
"type": "object",
"properties": {},
"required": []
"properties": {
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`.",
"x-priority": "internal"
},
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
}
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './add-github-action-ci';
export * from './add-gitignore-entry';
export * from './versions';
3 changes: 3 additions & 0 deletions src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { version } from '../../package.json';

export const nxFoundryVersion = version;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"nx-foundry": [
"src/index.ts"
]
}
},
"resolveJsonModule": true
},
"files": [],
"include": [],
Expand Down

0 comments on commit a5bda86

Please sign in to comment.