#!/usr/bin/env node 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(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(rootDir, module, 'package.json') // Read original contents of package.json const originalPackage = readFileSync(packagePath, 'utf-8') // Write to backup file // writeFileSync(packagePath + '.backup', originalPackage) // Parse package.json const p = JSON.parse(originalPackage) // Change package name p.name = `nuxt-edge${module ? `-${module}` : ''}` // Get latest git commit id const gitCommit = String( spawnSync('git', 'rev-parse --short HEAD'.split(' ')).stdout ).trim() // Version with latest git commit id // Using date.now() so latest push will be always choosen by npm/yarn const date = Math.round(Date.now() / (1000 * 60)) const baseVersion = p.version.split('-')[0] p.version = `${baseVersion}-${date}.${gitCommit}` 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 } static publish(module = '') { const p = this.changePackageName(module) // Parse git branch to decide npm tag let tag = String( spawnSync('git', 'rev-parse --abbrev-ref HEAD'.split(' ')).stdout ).trim() // dev ~> latest if (tag === 'dev') { tag = 'latest' } // Log // eslint-disable-next-line no-console console.log(`publishing ${p.name}@${p.version} with tag ${tag}`) // Do publish const { status, output } = spawnSync('npm', `publish --tag ${tag}`.split(' '), { cwd: resolve(rootDir, module) }) // eslint-disable-next-line no-console console.log(String(output.concat('\n')).trim()) if (status === 1) { process.exit(1) } } } // publish nuxt-edge NuxtEdgePublisher.publish() // Run make start // eslint-disable-next-line no-console console.log(`building nuxt-edge-start`) spawnSync('npm', 'run build:nuxt-start'.split(' ')) // publish nuxt-start NuxtEdgePublisher.publish('start')