Skip to content

Commit

Permalink
feat: preset support
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Dec 30, 2018
1 parent 5c0b5bb commit 4efa7c0
Show file tree
Hide file tree
Showing 20 changed files with 775 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module.exports = class MigrationOperation {
if (file) {
file.source = source
} else {
this.files[filename] = new GeneratorFile(filename, source, true)
this.files[filename] = new GeneratorFile(this.cwd, filename, source, true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ module.exports = class MigrationOperationAPI {
ext: ext.substr(1),
})
if (newFile !== file) {
files[newFile] = files[file]
const f = files[newFile] = files[file]
f.move(newFile)
delete files[file]
}
}
Expand Down
18 changes: 14 additions & 4 deletions packages/@nodepack/app-migrator/src/lib/MigrationOperationFile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const fs = require('fs')
const path = require('path')
const isBinary = require('isbinaryfile')

module.exports = class MigrationOperationFile {
/**
* @param {string} cwd
* @param {string} filename Path relative to project.
* @param {string | Buffer?} source
* @param {boolean} modified
*/
constructor (filename, source = null, modified = false) {
constructor (cwd, filename, source = null, modified = false) {
this.cwd = cwd
this.filename = filename
this._source = source
this.modified = modified
Expand All @@ -19,9 +22,10 @@ module.exports = class MigrationOperationFile {
*/
get source () {
if (!this._source) {
this._source = isBinary.sync(name)
? fs.readFileSync(name)
: fs.readFileSync(name, 'utf-8')
const filePath = path.resolve(this.cwd, this.filename)
this._source = isBinary.sync(filePath)
? fs.readFileSync(filePath)
: fs.readFileSync(filePath, 'utf-8')
}
return this._source
}
Expand All @@ -30,4 +34,10 @@ module.exports = class MigrationOperationFile {
this._source = value
this.modified = true
}

move (newName) {
// Copy original source
this.source = this.source
this.filename = newName
}
}
61 changes: 41 additions & 20 deletions packages/@nodepack/app-migrator/src/lib/Migrator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @typedef {import('@nodepack/service').ProjectOptions} ProjectOptions */
/** @typedef {import('./MigratorPlugin')} MigratorPlugin */
/** @typedef {import('../../types/MigrationOptions').MigrationOptions} MigrationOptions */
/** @typedef {import('@nodepack/utils').Preset} Preset */

/** @typedef {Object.<string, Object.<string, any>>} MigrationsOptions */

/**
* @typedef MigratorOptions
Expand Down Expand Up @@ -36,11 +39,6 @@ const MigrationAPI = require('./MigrationAPI')
const MigrationWhenAPI = require('./MigrationWhenAPI')
const MigrationOperation = require('./MigrationOperation')
// Utils
const {
ensureConfigFile,
readConfigFile,
writeConfigFile,
} = require('../util/configFiles')
const { readPkg } = require('../util/pkg')
const {
toShortPluginId,
Expand All @@ -50,6 +48,14 @@ const {
done,
warn,
error,
logWithSpinner,
stopSpinner,
chalk,
ensureConfigFile,
readConfigFile,
writeConfigFile,
FILE_APP_MIGRATIONS_PLUGIN_VERSIONS,
FILE_APP_MIGRATIONS_RECORDS,
} = require('@nodepack/utils')
const inquirer = require('inquirer')
const { getVersion } = require('../util/plugins')
Expand All @@ -62,9 +68,6 @@ const logTypes = {
error,
}

const APP_MIGRATIONS_RECORDS = 'app-migration-records.json'
const APP_MIGRATIONS_PLUGIN_VERSIONS = 'app-migration-plugin-versions.json'

module.exports = class Migrator {
/**
* @param {string} cwd
Expand All @@ -88,31 +91,47 @@ module.exports = class Migrator {
this.notices = []
}

async migrate ({
extractConfigFiles = false,
}) {
/**
* @param {Preset?} preset
*/
async migrate (preset = null) {
/** @type {MigrationsOptions?} */
let options = null
let extractConfigFiles = false

if (preset) {
extractConfigFiles = preset.useConfigFiles || false
if (preset.appMigrations && preset.appMigrations.options) {
options = preset.appMigrations.options
}
}

await this.setup()
await this.readPluginVersions()

const migrations = await this.resolveMigrations()

// Prompts
const rootOptions = await this.resolvePrompts(migrations)
const rootOptions = options || await this.resolvePrompts(migrations)

for (const migration of migrations) {
// Prompts results
const pluginOptions = rootOptions[migration.plugin.id]
const options = (pluginOptions && pluginOptions[migration.options.id]) || {}
const migrationOptions = (pluginOptions && pluginOptions[migration.options.id]) || {}

const operation = new MigrationOperation(this, migration, {
options,
options: migrationOptions,
rootOptions,
})

logWithSpinner('✔️', `${chalk.grey(migration.plugin.id)} ${migration.options.title}`)

await operation.migrate({
extractConfigFiles,
})

stopSpinner()

// Mark migration as completed
this.migrationRecords.push({
id: migration.options.id,
Expand Down Expand Up @@ -140,8 +159,8 @@ module.exports = class Migrator {
*/
async setup () {
// Ensure the config files exists in '.nodepack' folder
await ensureConfigFile(this.cwd, APP_MIGRATIONS_RECORDS, [])
await ensureConfigFile(this.cwd, APP_MIGRATIONS_PLUGIN_VERSIONS, {})
await ensureConfigFile(this.cwd, FILE_APP_MIGRATIONS_RECORDS, [])
await ensureConfigFile(this.cwd, FILE_APP_MIGRATIONS_PLUGIN_VERSIONS, {})

// Read package.json
this.pkg = readPkg(this.cwd)
Expand All @@ -165,7 +184,7 @@ module.exports = class Migrator {
* @private
*/
async readMigrationRecords () {
this.migrationRecords = await readConfigFile(this.cwd, APP_MIGRATIONS_RECORDS)
this.migrationRecords = await readConfigFile(this.cwd, FILE_APP_MIGRATIONS_RECORDS)
// Cache ids
for (const record of this.migrationRecords) {
this.migratedIds.set(`${record.pluginId}${record.id}`, true)
Expand All @@ -176,7 +195,7 @@ module.exports = class Migrator {
* @private
*/
async writeMigrationRecords () {
await writeConfigFile(this.cwd, APP_MIGRATIONS_RECORDS, this.migrationRecords)
await writeConfigFile(this.cwd, FILE_APP_MIGRATIONS_RECORDS, this.migrationRecords)
}

/**
Expand Down Expand Up @@ -217,8 +236,10 @@ module.exports = class Migrator {

/**
* @param {Migration []} migrations
* @returns {Promise.<MigrationsOptions>}
*/
async resolvePrompts (migrations) {
/** @type {MigrationsOptions} */
const rootOptions = {}
for (const migration of migrations) {
if (migration.options.prompts) {
Expand All @@ -244,7 +265,7 @@ module.exports = class Migrator {
* @private
*/
async readPluginVersions () {
const pluginVersions = await readConfigFile(this.cwd, APP_MIGRATIONS_PLUGIN_VERSIONS)
const pluginVersions = await readConfigFile(this.cwd, FILE_APP_MIGRATIONS_PLUGIN_VERSIONS)
for (const plugin of this.plugins) {
plugin.currentVersion = getVersion(plugin.id, this.cwd)
plugin.previousVersion = pluginVersions[plugin.id]
Expand Down Expand Up @@ -275,7 +296,7 @@ module.exports = class Migrator {
}
}

await writeConfigFile(this.cwd, APP_MIGRATIONS_PLUGIN_VERSIONS, result)
await writeConfigFile(this.cwd, FILE_APP_MIGRATIONS_PLUGIN_VERSIONS, result)
}

/**
Expand Down
59 changes: 0 additions & 59 deletions packages/@nodepack/app-migrator/src/util/configFiles.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/@nodepack/app-migrator/src/util/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.resolveFiles = async (cwd, pattern = '**', map = null) => {
* @returns {Promise.<FileTree>}
*/
exports.readFiles = async (cwd) => {
return exports.resolveFiles(cwd, '**', (file) => new MigrationOperationFile(file))
return exports.resolveFiles(cwd, '**', (file) => new MigrationOperationFile(cwd, file))
}

exports.normalizeFilePaths = (files) => {
Expand Down
16 changes: 11 additions & 5 deletions packages/@nodepack/app-migrator/src/util/writeFileTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs-extra')
const path = require('path')
const { error } = require('@nodepack/utils')

/**
* @param {string} cwd
Expand Down Expand Up @@ -32,11 +33,16 @@ module.exports = async function (cwd, files, previousFileNames) {
}

for (const filename of Object.keys(files)) {
const file = files[filename]
if (!file.modified) continue
const filePath = path.join(cwd, filename)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, file.source)
try {
const file = files[filename]
if (!file.modified) continue
const filePath = path.join(cwd, filename)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, file.source)
} catch (e) {
error(`Error while writing ${filename}`)
throw e
}
}
}

1 change: 1 addition & 0 deletions packages/@nodepack/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"chalk": "^2.4.1",
"cmd-shim": "^2.0.2",
"commander": "^2.19.0",
"download-git-repo": "^1.1.0",
"execa": "^1.0.0",
"fs-extra": "^7.0.1",
"inquirer": "^6.2.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/@nodepack/cli/src/bin/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ program
program
.command('create <app-name>')
.description('create a new project powered by nodepack')
.option('-p, --preset <presetName>', 'Skip prompts and use saved or remote preset')
.option('-d, --default', 'Skip prompts and use the default preset')
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.option('-g, --git [message]', 'Force git initialization with initial commit message')
Expand Down
Loading

0 comments on commit 4efa7c0

Please sign in to comment.