Skip to content

Commit

Permalink
fix(misc): cleanup migration to workspace-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 21, 2023
1 parent 06a885a commit be768ca
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/plugin/generators/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"publishable": {
"type": "boolean",
"description": "Generates a boilerplate for publishing the plugin to npm.",
"default": false
}
},
"required": ["name"],
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/generators/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function pluginGenerator(host: Tree, schema: Schema) {
...schema,
config: 'project',
bundler: options.bundler,
publishable: true,
publishable: options.publishable,
importPath: options.npmPackageName,
skipFormat: true,
})
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/generators/plugin/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface Schema {
setParserOptionsProject?: boolean;
compiler: 'swc' | 'tsc';
rootProject?: boolean;
publishable?: boolean;
}
5 changes: 5 additions & 0 deletions packages/plugin/src/generators/plugin/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"publishable": {
"type": "boolean",
"description": "Generates a boilerplate for publishing the plugin to npm.",
"default": false
}
},
"required": ["name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface NormalizedSchema extends Schema {
npmScope: string;
npmPackageName: string;
bundler: 'swc' | 'tsc';
publishable: boolean;
}
export function normalizeOptions(
host: Tree,
Expand Down Expand Up @@ -58,5 +59,6 @@ export function normalizeOptions(
projectDirectory: fullProjectDirectory,
parsedTags,
npmPackageName,
publishable: options.publishable ?? false,
};
}
1 change: 1 addition & 0 deletions packages/plugin/src/generators/preset/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function (tree: Tree, options: PresetGeneratorSchema) {
importPath: options.pluginName,
rootProject: true,
e2eTestRunner: 'jest',
publishable: true,
});
tasks.push(pluginTask);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ const PROJECT_NAME = 'workspace-plugin';
const DESTINATION = `tools/${PROJECT_NAME}`;

export default async function (tree: Tree) {
const tasks = [];
if (!tree.children('tools/generators').length) {
if (!tree.exists('tools/generators')) {
return;
}
const tasks = [];
if (hasWorkspaceGenerators(tree)) {
tasks.push(...(await moveWorkspaceGeneratorsToLocalPlugin(tree)));
}
removeToolsGeneratorsIfEmpty(tree);
await formatFiles(tree);
return () => {
for (const task of tasks) {
task();
}
};
}

async function moveWorkspaceGeneratorsToLocalPlugin(tree: Tree) {
const tasks = [];
let project = getProjects(tree).get(PROJECT_NAME);
if (!project) {
await createNewPlugin(tree);
Expand All @@ -48,21 +61,19 @@ export default async function (tree: Tree) {
project = readProjectConfiguration(tree, PROJECT_NAME);
}
await updateExistingPlugin(tree, project);
removeToolsGeneratorsIfEmpty(tree);
await formatFiles(tree);
return () => {
for (const task of tasks) {
task();
}
};
return tasks;
}

function removeToolsGeneratorsIfEmpty(tree: Tree) {
function hasWorkspaceGenerators(tree: Tree) {
const children = tree.children('tools/generators');
if (
children.length === 0 ||
(children.length === 1 && children[0] === '.gitkeep')
) {
return (
children.length > 0 &&
!(children.length === 1 && children[0] === '.gitkeep')
);
}

function removeToolsGeneratorsIfEmpty(tree: Tree) {
if (!hasWorkspaceGenerators(tree)) {
tree.delete('tools/generators');
}
}
Expand Down Expand Up @@ -174,6 +185,7 @@ async function createNewPlugin(tree: Tree) {
skipLintChecks: false,
unitTestRunner: 'jest',
e2eTestRunner: 'none',
publishable: false,
});
getCreateGeneratorsJson()(
tree,
Expand Down

0 comments on commit be768ca

Please sign in to comment.