Skip to content

Commit

Permalink
refactor: update nuxt-start dependencies dynamically (nuxt#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo authored and pi0 committed Aug 30, 2018
1 parent aeb33e0 commit 4311aca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
36 changes: 29 additions & 7 deletions scripts/release-edge
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,39 @@ const { resolve, join } = require('path')
const { spawnSync } = require('child_process')
const { readFileSync, writeFileSync, copySync } = require('fs-extra')

const rootDir = resolve(__dirname, '..')

class NuxtEdgePublisher {
static copyFiles(moduleName, src, fieNames) {
const srcDir = resolve(__dirname, '..', src)
const moduleDir = resolve(__dirname, '..', moduleName, src)
const srcDir = resolve(rootDir, src)
const moduleDir = resolve(rootDir, moduleName, src)
for (const file of fieNames) {
copySync(resolve(srcDir, file), resolve(moduleDir, file))
}
}

static updateDeps(moduleName, packageObj, bundleFile) {
const { dependencies: rootDeps } = JSON.parse(readFileSync(resolve(rootDir, 'package.json'), 'utf-8'))
// Required and Excluded packages for start
const requires = {'minimist': true}
const requireRegex = /require\('([-@/\w]+)'\)/g
const rawCore = readFileSync(resolve(rootDir, bundleFile))
let match = requireRegex.exec(rawCore)
while (match) {
requires[match[1]] = true
match = requireRegex.exec(rawCore)
}

for (const dep in rootDeps) {
if (requires[dep]) {
packageObj.dependencies[dep] = rootDeps[dep]
}
}
}

static changePackageName(module = '') {
// paths
const packagePath = resolve(__dirname, '..', module, 'package.json')
const packagePath = resolve(rootDir, module, 'package.json')

// Read original contents of package.json
const originalPackage = readFileSync(packagePath, 'utf-8')
Expand All @@ -40,14 +61,15 @@ class NuxtEdgePublisher {
const baseVersion = p.version.split('-')[0]
p.version = `${baseVersion}-${date}.${gitCommit}`

// Write package.json
writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n')

if (module === 'start') {
this.updateDeps('start', p, join('dist', 'nuxt-start.js'))
this.copyFiles(module, 'dist', ['nuxt-start.js'])
this.copyFiles(module, 'bin', ['nuxt-start', join('common', 'utils.js')])
}

// Write package.json
writeFileSync(packagePath, JSON.stringify(p, null, 2) + '\r\n')

return p
}

Expand All @@ -70,7 +92,7 @@ class NuxtEdgePublisher {

// Do publish
const { status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' '), {
cwd: resolve(__dirname, '..', module)
cwd: resolve(rootDir, module)
})

// eslint-disable-next-line no-console
Expand Down
1 change: 0 additions & 1 deletion start/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*
!README.md
!package.json
22 changes: 0 additions & 22 deletions start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@
"npm": ">=5.0.0"
},
"dependencies": {
"@nuxtjs/youch": "^4.2.3",
"chalk": "^2.3.2",
"compression": "^1.7.3",
"connect": "^3.6.5",
"consola": "^1.4.3",
"esm": "^3.0.10",
"etag": "^1.8.1",
"fresh": "^0.5.2",
"fs-extra": "^7.0.0",
"hash-sum": "^1.0.2",
"ip": "^1.1.5",
"launch-editor-middleware": "^2.2.1",
"lodash": "^4.17.5",
"lru-cache": "^4.1.3",
"minimist": "^1.2.0",
"serialize-javascript": "^1.4.0",
"serve-static": "^1.13.2",
"server-destroy": "^1.0.1",
"std-env": "^1.3.1",
"vue": "^2.5.17",
"vue-meta": "^1.5.3",
"vue-server-renderer": "^2.5.17"
},
"collective": {
"type": "opencollective",
Expand Down

0 comments on commit 4311aca

Please sign in to comment.