-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schematics): do not delete inexisting files (#1629)
## Related issues - 🐛 Fixes #1618
- Loading branch information
Showing
1 changed file
with
36 additions
and
23 deletions.
There are no files selected for viewing
59 changes: 36 additions & 23 deletions
59
packages/@o3r/workspace/schematics/sdk/rules/clean-standalone.rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
]); | ||
} |