Skip to content

Commit

Permalink
fix(schematics): do not delete inexisting files (#1629)
Browse files Browse the repository at this point in the history
## Related issues

- 🐛 Fixes #1618
  • Loading branch information
matthieu-crouzet authored Apr 11, 2024
2 parents 52e0add + ee3c538 commit 7cfd0ea
Showing 1 changed file with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
import type { Rule } from '@angular-devkit/schematics';
import { chain, type Rule } from '@angular-devkit/schematics';
import { posix } from 'node:path';
import type { PackageJson } from 'type-fest';

const deleteIfExists = (paths: string[]): Rule => (tree, context) => {
paths.forEach((path) => {
if (tree.exists(path)) {
tree.delete(path);
} else {
context.logger.warn(`Tried to delete ${path} but file does not exist.`);
}
});
};

/**
* Clean files generated to standalone SDK
* @param targetPath Path where the SDK has been generated
*/
export function cleanStandaloneFiles(targetPath: string): Rule {
return (tree) => {
tree.delete(posix.join(targetPath, '.renovaterc.json'));
tree.delete(posix.join(targetPath, '.vscode', 'settings.json'));
tree.delete(posix.join(targetPath, '.editorconfig'));
tree.delete(posix.join(targetPath, '.versionrc.json'));
tree.delete(posix.join(targetPath, '.commitlintrc.json'));
tree.delete(posix.join(targetPath, 'CONTRIBUTING.md'));
tree.delete(posix.join(targetPath, '.husky', 'commit-msg'));
tree.delete(posix.join(targetPath, '.husky', 'pre-commit'));

const packageJson = tree.readJson(posix.join(targetPath, 'package.json')) as PackageJson;
if (packageJson.scripts) {
const excludedScripts = ['postinstall', 'set:version'];
packageJson.scripts = Object.fromEntries(
Object.entries(packageJson.scripts).filter(([scriptName]) => !excludedScripts.includes(scriptName))
);
}
if (packageJson.devDependencies) {
packageJson.devDependencies = Object.fromEntries(Object.entries(packageJson.devDependencies).filter(([depName, _]) => depName !== '@o3r/workspace'));
return chain([
deleteIfExists(
[
posix.join(targetPath, '.renovaterc.json'),
posix.join(targetPath, '.vscode', 'settings.json'),
posix.join(targetPath, '.editorconfig'),
posix.join(targetPath, '.versionrc.json'),
posix.join(targetPath, '.commitlintrc.json'),
posix.join(targetPath, 'CONTRIBUTING.md')
]
),
(tree) => {
const packageJson = tree.readJson(posix.join(targetPath, 'package.json')) as PackageJson;
if (packageJson.scripts) {
const excludedScripts = ['postinstall', 'set:version'];
packageJson.scripts = Object.fromEntries(
Object.entries(packageJson.scripts).filter(([scriptName]) => !excludedScripts.includes(scriptName))
);
}
if (packageJson.devDependencies) {
packageJson.devDependencies = Object.fromEntries(Object.entries(packageJson.devDependencies).filter(([depName]) => depName !== '@o3r/workspace'));
}
tree.overwrite(posix.join(targetPath, 'package.json'), JSON.stringify(packageJson, null, 2));
return tree;
}
tree.overwrite(posix.join(targetPath, 'package.json'), JSON.stringify(packageJson, null, 2));
return tree;
};
]);
}

0 comments on commit 7cfd0ea

Please sign in to comment.