Skip to content

Commit 033abb4

Browse files
committed
fix: throw error when main is unset at package.json
The error message is confusing when main is unset in package.json of template. resolve #44
1 parent 94d8afd commit 033abb4

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/commands/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = async (options = {}) => {
4343

4444
config = await config.reload({
4545
templateVersion: version,
46+
loadTemplate: true,
4647
})
4748

4849
checkParams.engine(config)

src/commands/update.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ module.exports = async (options) => {
8282
await downloadTemplate(config.template.repository, version)
8383
config = await config.reload({
8484
templateVersion: version,
85+
loadTemplate: true,
8586
})
8687
checkParams.engine(config)
8788

src/commands/upgrade.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = async (options = {}) => {
3232
await downloadTemplate(config.template.repository, version)
3333
config = await config.reload({
3434
templateVersion: version,
35+
loadTemplate: true,
3536
})
3637
checkParams.engine(config)
3738

src/load-config/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const loadTemplateConfig = require('./load-template-config')
33
const loadProjectConfig = require('./load-project-config')
44

55

6-
const loadConfig = async ({ cwd, projectName, templateRepository, templateVersion }) => {
6+
const loadConfig = async ({ cwd, projectName, templateRepository, templateVersion, loadTemplate }) => {
77
const mili = await loadMiliConfig()
8-
const template = await loadTemplateConfig(templateRepository, templateVersion)
8+
const template = await loadTemplateConfig(templateRepository, templateVersion, loadTemplate)
99
const project = await loadProjectConfig(cwd, projectName)
1010

1111

src/load-config/load-template-config/index.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const loadEntryFile = async path => {
3333
try {
3434
const packageJson = await loadPackageJson(path)
3535
let entryFilePath = join(path, 'entry.js')
36-
if (packageJson) entryFilePath = join(path, packageJson.main)
36+
if (packageJson && typeof packageJson.main === 'string') entryFilePath = join(path, packageJson.main)
3737

3838
const result = await cosmiconfig('template').load(entryFilePath)
3939
const config = checkConfig(result.config)
@@ -55,7 +55,7 @@ const loadEntryFile = async path => {
5555
}
5656

5757

58-
module.exports = async (repository, version) => {
58+
module.exports = async (repository, version, load = false) => {
5959
const milirc = await loadMilirc()
6060

6161
if (!repository) {
@@ -80,34 +80,31 @@ module.exports = async (repository, version) => {
8080
interactionSHA1: '',
8181
}
8282

83-
if (!repository) return config
83+
if (!load) return config
8484

8585
const localStoragePath = await getLocalStoragePath(config.repository, config.version)
8686
if (!await fs.pathExists(localStoragePath)) return config
8787

8888
const packageJson = await loadPackageJson(localStoragePath)
89-
try {
90-
const entryFile = await loadEntryFile(localStoragePath)
91-
config.name = entryFile.name || packageJson.name || ''
92-
config.version = version
93-
config.engines = entryFile.engines
9489

95-
config.path = join(localStoragePath, entryFile.path)
96-
if (!await isDirectory(config.path)) throwError('The template path should be a folder')
90+
const entryFile = await loadEntryFile(localStoragePath)
91+
config.name = entryFile.name || packageJson.name || ''
92+
config.version = version
93+
config.engines = entryFile.engines
9794

98-
config.rules = entryFile.rules.map(rule => {
99-
rule.path = join(localStoragePath, rule.path)
100-
return formatRule(rule)
101-
})
95+
config.path = join(localStoragePath, entryFile.path)
96+
if (!await isDirectory(config.path)) throwError('The template path should be a folder')
10297

103-
config.interaction = entryFile.interaction
104-
config.interactionSHA1 = sha1(JSON.stringify(entryFile.interaction))
105-
config.hooks = formatHooks(entryFile.hooks)
98+
config.rules = entryFile.rules.map(rule => {
99+
rule.path = join(localStoragePath, rule.path)
100+
return formatRule(rule)
101+
})
106102

107-
config.files = await searchFile(config)
108-
config.status = 'loaded'
109-
return config
110-
} catch (e) {
111-
return config
112-
}
103+
config.interaction = entryFile.interaction
104+
config.interactionSHA1 = sha1(JSON.stringify(entryFile.interaction))
105+
config.hooks = formatHooks(entryFile.hooks)
106+
107+
config.files = await searchFile(config)
108+
config.status = 'loaded'
109+
return config
113110
}

0 commit comments

Comments
 (0)