Skip to content

Commit cd7ed47

Browse files
committed
fix: template.path error when reload npm template config
1 parent 9c147e3 commit cd7ed47

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

.lintstagedrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
'**/*.{js,jsx}':
1+
'**/*.{ts}':
22
- 'eslint --fix'
33
- 'git add'

src/class/repository/npm-repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { Effect } from '@/internal'
1010
const genIndexFile = (name: string): string => `
1111
const { join } = require('path')
1212
const config = require('${name}')
13-
config.path = join('./node_modules/${name}', config.path)
13+
const path = join('./node_modules/${name}', config.path)
1414
15-
module.exports = config
15+
module.exports = { ...config, path }
1616
`
1717

1818
const exec = promisify(childProcess.exec)

src/class/template.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Ajv from 'ajv'
22
import ajvKeywords from 'ajv-keywords'
33
import { join, dirname } from 'path'
44
import cosmiconfig from 'cosmiconfig'
5-
import { unnest } from 'ramda'
5+
import { unnest, clone } from 'ramda'
66
import { loadNpmConfig } from '@/loader'
77
import { TemplateSchema, RuleSchema, QuestionSchema } from '@/schema'
88
import { isDirectory, logger } from '@/utils'
@@ -119,7 +119,6 @@ export class Template {
119119

120120
public static async load(repo: Repository): Promise<Template> {
121121
const cwd = repo.storage
122-
123122
let entry = join(cwd, 'index.js')
124123

125124
try {
@@ -128,6 +127,10 @@ export class Template {
128127
} catch (e) {
129128
}
130129

130+
/**
131+
* NOTE: don't change the object that return by cosmiconfig.
132+
* It was cache by cosmiconfig
133+
*/
131134
const result = await cosmiconfig('template').load(entry)
132135
if (!result) {
133136
throw new Error([
@@ -136,7 +139,7 @@ export class Template {
136139
].join('\n'))
137140
}
138141

139-
const config = result.config
142+
const config = clone(result.config)
140143

141144
const valid = validateTemplateConfig(config)
142145

@@ -148,15 +151,15 @@ export class Template {
148151
}
149152

150153
/** absoult template path */
151-
config.path = join(cwd, config.path)
154+
const path = join(cwd, config.path)
152155

153156
/** generate files */
154157
const rules = config.rules
155-
.map(item => ({ ...item, path: join(config.path, item.path) }))
158+
.map(item => ({ ...item, path: join(path, item.path) }))
156159
.map(item => Rule.format(item))
157-
const rootRule = new Rule(config.path, UpgradeType.Cover, true)
160+
const rootRule = new Rule(path, UpgradeType.Cover, true)
158161

159-
const files = await this.searchDirFile(config.path, rootRule, rules)
162+
const files = await this.searchDirFile(path, rootRule, rules)
160163

161164
/** generate questions */
162165
let questions: Questions = []
@@ -170,7 +173,7 @@ export class Template {
170173
}
171174

172175

173-
return new Template(repo, config.path, config.engines, files, questions, hooks)
176+
return new Template(repo, path, config.engines, files, questions, hooks)
174177
}
175178

176179

src/loader/load-milirc.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Ajv from 'ajv'
55
import { relativePath, isRelativePath } from '@/utils'
66
import { MilircSchema } from '@/schema'
77
import { Milirc, Effect } from '@/internal'
8+
import { clone } from 'ramda'
89

910

1011
const ajv = new Ajv({ useDefaults: true, nullable: true })
@@ -13,28 +14,29 @@ const validate = ajv.compile(MilircSchema)
1314
const explorer = cosmiconfig('mili')
1415

1516
export default async(cwd: string): Promise<Milirc | null> => {
16-
let config: Milirc | null = null
17-
1817
const filepath = join(cwd, '.milirc.yml')
19-
if (!await Effect.fs.pathExists(filepath)) return config
18+
if (!await Effect.fs.pathExists(filepath)) return null
2019

20+
/**
21+
* NOTE: don't change the object that return by cosmiconfig.
22+
* It was cache by cosmiconfig
23+
*/
2124
const result = await explorer.load(filepath)
22-
if (!result || !result.config) return config
23-
24-
config = JSON.parse(JSON.stringify(result.config))
25+
if (!result || !result.config) return null
2526

27+
const config = clone(result.config)
2628
if (!config) throw new Error('Cannot load .milirc.yml')
2729

28-
if (config.mili && semver.lt(config.mili.version, '2.0.0')) throw new Error('Never support auto upgrade from mili@1')
29-
3030
const valid = validate(config)
31-
3231
if (!valid) throw new Error(ajv.errorsText(validate.errors, { dataVar: 'milirc' }))
32+
const milirc: Milirc = config as Milirc
33+
34+
if (semver.lt(milirc.mili.version, '2.0.0')) throw new Error('Never support auto upgrade from mili@1')
3335

3436
/** The relative template path saved in .milirc is relative to the dir of .milirc */
3537
if (isRelativePath(config.template.repository)) {
3638
config.template.repository = relativePath(process.cwd(), join(cwd, config.template.repository))
3739
}
3840

39-
return config
41+
return milirc
4042
}

0 commit comments

Comments
 (0)