2017-07-13 10:10:12 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-07-30 16:24:53 +00:00
|
|
|
const now = Date.now()
|
|
|
|
|
2018-03-27 22:28:17 +00:00
|
|
|
const { resolve } = require('path')
|
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
const {
|
|
|
|
readFileSync,
|
|
|
|
readJSONSync,
|
|
|
|
writeFileSync,
|
|
|
|
copySync,
|
|
|
|
removeSync
|
|
|
|
} = require('fs-extra')
|
2017-07-13 10:10:12 +00:00
|
|
|
|
|
|
|
// Dirs
|
|
|
|
const rootDir = resolve(__dirname, '..')
|
|
|
|
const startDir = resolve(rootDir, 'start')
|
|
|
|
|
|
|
|
// Read main package.json
|
|
|
|
const packageJSON = readJSONSync(resolve(rootDir, 'package.json'))
|
|
|
|
|
|
|
|
// Required and Excluded packages for start
|
2018-03-16 17:02:56 +00:00
|
|
|
let requires = ['minimist']
|
2017-07-22 18:03:54 +00:00
|
|
|
|
2018-03-27 22:28:17 +00:00
|
|
|
const excludes = ['path', 'fs', 'http', 'module', 'crypto'].concat(
|
2018-01-13 05:22:11 +00:00
|
|
|
Object.keys(packageJSON.devDependencies)
|
|
|
|
)
|
2017-07-13 10:10:12 +00:00
|
|
|
|
|
|
|
// Parse dist/core.js for all external dependencies
|
2017-08-05 00:58:26 +00:00
|
|
|
const requireRegex = /require\('([-@/\w]+)'\)/g
|
2018-03-27 22:28:17 +00:00
|
|
|
const rawCore = readFileSync(resolve(rootDir, 'dist/nuxt-start.js'))
|
2017-07-13 10:10:12 +00:00
|
|
|
let match = requireRegex.exec(rawCore)
|
|
|
|
while (match) {
|
|
|
|
requires.push(match[1])
|
|
|
|
match = requireRegex.exec(rawCore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply Excludes
|
|
|
|
requires = requires.filter(r => excludes.indexOf(r) === -1)
|
|
|
|
|
|
|
|
// Resolve version constrains
|
|
|
|
let dependencies = {}
|
|
|
|
requires.forEach(r => {
|
|
|
|
if (!packageJSON.dependencies[r]) {
|
2017-11-25 13:52:42 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2017-07-30 16:24:53 +00:00
|
|
|
console.warn('Cannot resolve dependency version for ' + r)
|
2017-07-13 10:10:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dependencies[r] = packageJSON.dependencies[r]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Drop fields
|
|
|
|
let drops = ['devDependencies', 'scripts', 'nyc', 'types']
|
|
|
|
drops.forEach(k => {
|
|
|
|
delete packageJSON[k]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Update dependencies
|
|
|
|
packageJSON.dependencies = dependencies
|
|
|
|
|
|
|
|
// Update package meta
|
2018-03-27 22:28:17 +00:00
|
|
|
packageJSON.name = packageJSON.name + '-start'
|
2017-07-13 10:10:12 +00:00
|
|
|
packageJSON.description = 'runtime-only build for nuxt'
|
2017-08-17 17:40:54 +00:00
|
|
|
packageJSON.bin = {
|
|
|
|
'nuxt-start': './bin/nuxt-start'
|
|
|
|
}
|
2017-07-13 10:10:12 +00:00
|
|
|
|
|
|
|
// Update package.json
|
2018-01-13 05:22:11 +00:00
|
|
|
writeFileSync(
|
|
|
|
resolve(startDir, 'package.json'),
|
|
|
|
JSON.stringify(packageJSON, null, 2)
|
|
|
|
)
|
2017-07-13 10:10:12 +00:00
|
|
|
|
|
|
|
// Copy required files
|
2018-01-13 05:22:11 +00:00
|
|
|
const excludeFiles = ['README.md', '.gitignore']
|
2017-07-13 10:10:12 +00:00
|
|
|
packageJSON.files.forEach(file => {
|
|
|
|
if (excludeFiles.indexOf(file) !== -1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let src = resolve(rootDir, file)
|
|
|
|
let dst = resolve(startDir, file)
|
|
|
|
// console.log(relative(rootDir, src), '~>', relative(rootDir, dst))
|
|
|
|
removeSync(dst)
|
|
|
|
copySync(src, dst)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Remove extras
|
|
|
|
const extraFiles = [
|
|
|
|
'bin/nuxt-build',
|
|
|
|
'bin/nuxt-generate',
|
|
|
|
'bin/nuxt-dev',
|
2017-07-30 16:24:53 +00:00
|
|
|
'bin/nuxt',
|
2017-07-13 10:10:12 +00:00
|
|
|
'dist/nuxt.js',
|
|
|
|
'dist/nuxt.js.map'
|
|
|
|
]
|
|
|
|
extraFiles.forEach(file => {
|
|
|
|
removeSync(resolve(startDir, file))
|
|
|
|
})
|
|
|
|
|
2017-07-30 16:24:53 +00:00
|
|
|
// Patch bin/nuxt-start
|
|
|
|
const binStart = resolve(startDir, 'bin/nuxt-start')
|
2018-01-13 05:22:11 +00:00
|
|
|
writeFileSync(
|
|
|
|
binStart,
|
|
|
|
String(readFileSync(binStart)).replace(/nuxt start/g, 'nuxt-start')
|
|
|
|
)
|
2017-07-30 16:24:53 +00:00
|
|
|
|
2017-11-25 13:52:42 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2018-01-13 05:22:11 +00:00
|
|
|
console.log(
|
|
|
|
`Generated ${packageJSON.name}@${packageJSON.version} in ${Date.now() -
|
|
|
|
now}ms`
|
|
|
|
)
|