@@ -42,37 +42,57 @@ function removePackageKeys(
4242 return changed
4343}
4444
45- function sanitizeRepositoryPackageJson ( projectFolder : string ) : void {
46- const packageJsonPath = resolve ( projectFolder , 'package.json' )
45+ type DependencyGroup = Record < string , unknown > | undefined
46+
47+ type PackageJsonShape = {
48+ scripts ?: Record < string , string | undefined >
49+ dependencies ?: DependencyGroup
50+ devDependencies ?: DependencyGroup
51+ optionalDependencies ?: DependencyGroup
52+ peerDependencies ?: DependencyGroup
53+ }
4754
48- try {
49- const packageJson = JSON . parse ( readFileSync ( packageJsonPath , 'utf8' ) )
50- const scripts = packageJson . scripts as Record < string , string | undefined > | undefined
51- let changed = false
52-
53- if ( scripts ) {
54- for ( const scriptName of TOOLING_SCRIPTS_TO_REMOVE ) {
55- if ( scripts [ scriptName ] !== undefined ) {
56- scripts [ scriptName ] = undefined
57- changed = true
58- }
55+ // Strip the husky/lint-staged/commitlint tooling scripts and dependencies from a parsed
56+ // package.json (mutates in place). Returns whether anything was removed.
57+ function stripToolingEntries (
58+ packageJson : PackageJsonShape ,
59+ scripts : Record < string , string | undefined > | undefined ,
60+ ) : boolean {
61+ let changed = false
62+
63+ if ( scripts ) {
64+ for ( const scriptName of TOOLING_SCRIPTS_TO_REMOVE ) {
65+ if ( scripts [ scriptName ] !== undefined ) {
66+ scripts [ scriptName ] = undefined
67+ changed = true
5968 }
6069 }
70+ }
6171
62- const dependencyGroups : Array < Record < string , unknown > | undefined > = [
63- packageJson . dependencies ,
64- packageJson . devDependencies ,
65- packageJson . optionalDependencies ,
66- packageJson . peerDependencies ,
67- ]
72+ const dependencyGroups : DependencyGroup [ ] = [
73+ packageJson . dependencies ,
74+ packageJson . devDependencies ,
75+ packageJson . optionalDependencies ,
76+ packageJson . peerDependencies ,
77+ ]
6878
69- for ( const group of dependencyGroups ) {
70- if ( removePackageKeys ( group , TOOLING_PACKAGES_TO_REMOVE ) ) {
71- changed = true
72- }
79+ for ( const group of dependencyGroups ) {
80+ if ( removePackageKeys ( group , TOOLING_PACKAGES_TO_REMOVE ) ) {
81+ changed = true
7382 }
83+ }
84+
85+ return changed
86+ }
7487
75- if ( changed ) {
88+ function sanitizeRepositoryPackageJson ( projectFolder : string ) : void {
89+ const packageJsonPath = resolve ( projectFolder , 'package.json' )
90+
91+ try {
92+ const packageJson = JSON . parse ( readFileSync ( packageJsonPath , 'utf8' ) )
93+ const scripts = packageJson . scripts as Record < string , string | undefined > | undefined
94+
95+ if ( stripToolingEntries ( packageJson , scripts ) ) {
7696 writeFileSync ( packageJsonPath , `${ JSON . stringify ( packageJson , null , 2 ) } \n` )
7797 }
7898 } catch {
@@ -159,28 +179,12 @@ function patchPackageJsonCanton(
159179 scripts [ name ] = undefined
160180 }
161181 }
162-
163- // The husky `prepare` hook and commitlint scripts only leave with the pre-commit feature.
164- if ( precommitRemoved ) {
165- for ( const scriptName of TOOLING_SCRIPTS_TO_REMOVE ) {
166- if ( scripts [ scriptName ] !== undefined ) {
167- scripts [ scriptName ] = undefined
168- }
169- }
170- }
171182 }
172183
184+ // The husky tooling (prepare/commitlint scripts + husky/lint-staged/commitlint deps) only leaves
185+ // with the pre-commit feature.
173186 if ( precommitRemoved ) {
174- const dependencyGroups : Array < Record < string , unknown > | undefined > = [
175- packageJson . dependencies ,
176- packageJson . devDependencies ,
177- packageJson . optionalDependencies ,
178- packageJson . peerDependencies ,
179- ]
180-
181- for ( const group of dependencyGroups ) {
182- removePackageKeys ( group , TOOLING_PACKAGES_TO_REMOVE )
183- }
187+ stripToolingEntries ( packageJson , scripts )
184188 }
185189
186190 writeFileSync ( packageJsonPath , `${ JSON . stringify ( packageJson , null , 2 ) } \n` )
0 commit comments