1 | const fs = require('fs-extra')
|
2 | const { join } = require('path')
|
3 | const { shellSync } = require('execa')
|
4 | const { __nodeModules, __fireConfig } = require('./paths')
|
5 | exports.isFireProject = function() {
|
6 | const cwd = process.cwd()
|
7 | const { dependencies = {} } = require(join(cwd, 'package.json'))
|
8 | return (
|
9 | fs.pathExistsSync(join(cwd, 'fire.config.js')) &&
|
10 | fs.pathExistsSync(join(cwd, 'src')) &&
|
11 | fs.pathExistsSync(join(cwd, 'page')) &&
|
12 | dependencies['fire-scripts']
|
13 | )
|
14 | }
|
15 |
|
16 | exports.setFireConfig = function(name) {
|
17 | if (exports.isFireProject()) {
|
18 | if (!fs.pathExistsSync(__fireConfig)) {
|
19 | fs.writeFileSync(
|
20 | __fireConfig,
|
21 | `
|
22 | module.exports = {
|
23 | page: ['${name}']
|
24 | }
|
25 | `.trim()
|
26 | )
|
27 | } else {
|
28 | const res = fs.readFileSync(__fireConfig, 'utf8')
|
29 | fs.writeFileSync(
|
30 | __fireConfig,
|
31 | res.replace(
|
32 | /page\s*:\s*((\[(.+?)\])|('(.+?)')|("(.+?)"))\s*,?/g,
|
33 | `page:['${name}'],`
|
34 | )
|
35 | )
|
36 | }
|
37 | }
|
38 | }
|
39 | exports.setPkgJson = function(projectName) {
|
40 | const latestVer = shellSync('npm view fire-scripts version').stdout
|
41 | const { stdout } = shellSync('git config user.name')
|
42 | const packagePath = join(process.cwd(), projectName, 'package.json')
|
43 | const pkgJson = require(packagePath)
|
44 | pkgJson.name = projectName
|
45 | pkgJson.author = stdout
|
46 | pkgJson.dependencies['fire-scripts'] = latestVer
|
47 | fs.writeFileSync(packagePath, JSON.stringify(pkgJson, null, 2))
|
48 | }
|
49 | exports.checkFireVersion = function() {
|
50 | const currentVer = require(join(__nodeModules, 'fire-scripts/package.json'))
|
51 | .version
|
52 | try {
|
53 | const { stdout } = shellSync('npm view fire-scripts version')
|
54 | if (stdout !== currentVer) {
|
55 | return {
|
56 | currentVer,
|
57 | latestVer: stdout
|
58 | }
|
59 | }
|
60 | return false
|
61 | } catch (e) {
|
62 | return false
|
63 | }
|
64 | }
|