Skip to content

Commit 00a598d

Browse files
author
Guillaume Chau
committed
fix: improved git auto commit
1 parent dba6f50 commit 00a598d

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

packages/@nodepack/cli/src/lib/PluginAddJob.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = class PluginAddJob {
3535
const maintenance = new Maintenance({
3636
cwd,
3737
cliOptions,
38+
skipCommit: true,
3839
skipPreInstall: true,
3940
before: async ({ pkg, plugins, shouldCommitState, isTestOrDebug }) => {
4041
// Plugins
@@ -53,7 +54,7 @@ module.exports = class PluginAddJob {
5354
plugins.push(packageName)
5455
}
5556
} else if ((!alreadyInPkg || cliOptions.forceInstall) && !cliOptions.noInstall) {
56-
await shouldCommitState()
57+
await shouldCommitState(`[nodepack] before add ${packageName}`)
5758
log()
5859
log(`📦 Installing ${chalk.cyan(packageName)}...`)
5960
log()
@@ -70,7 +71,8 @@ module.exports = class PluginAddJob {
7071
process.exit(1)
7172
}
7273
},
73-
after: async maintenance => {
74+
after: async ({ shouldCommitState }) => {
75+
await shouldCommitState(`[nodepack] after add ${packageName}`)
7476
log(`🎉 Successfully added ${chalk.yellow(packageName)}.`)
7577
},
7678
})

packages/@nodepack/cli/src/lib/PluginUpgradeJob.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = class PluginUpgradeJob {
6161
const maintenance = new Maintenance({
6262
cwd,
6363
cliOptions,
64+
skipCommit: true,
6465
skipPreInstall: true,
6566
before: async ({ pkg, shouldCommitState, installDeps, isTestOrDebug }) => {
6667
logWithSpinner(`🔄`, `Checking for plugin updates...`)
@@ -115,17 +116,18 @@ module.exports = class PluginUpgradeJob {
115116
}))
116117
}
117118

118-
if (queuedUpdates.length) {
119+
const count = queuedUpdates.length
120+
if (count) {
119121
if (!cliOptions.yes) {
120122
const { confirm } = await inquirer.prompt([{
121123
name: 'confirm',
122124
type: 'confirm',
123-
message: `Confirm ${queuedUpdates.length} plugin update${queuedUpdates.length > 1 ? 's' : ''}?`,
125+
message: `Confirm ${count} plugin update${count > 1 ? 's' : ''}?`,
124126
}])
125127
if (!confirm) process.exit()
126128
}
127129

128-
await shouldCommitState()
130+
await shouldCommitState(`[nodepack] before update ${count} plugin${count > 1 ? 's' : ''}`)
129131

130132
for (const update of queuedUpdates) {
131133
pkg[update.info.dependencyType][update.info.id] = update.version
@@ -135,11 +137,15 @@ module.exports = class PluginUpgradeJob {
135137
if (!isTestOrDebug) {
136138
await installDeps(`📦 Updating packages...`)
137139
}
140+
} else {
141+
log(`${chalk.green('✔')} No plugin updates applied.`)
142+
process.exit()
138143
}
139144
}
140145
},
141-
after: async maintenance => {
146+
after: async ({ shouldCommitState }) => {
142147
const count = queuedUpdates.length
148+
await shouldCommitState(`[nodepack] after update ${chalk.yellow(`${count} plugin${count > 1 ? 's' : ''}`)}`)
143149
log(`🎉 Successfully upgraded ${chalk.yellow(`${count} plugin${count > 1 ? 's' : ''}`)}.`)
144150
},
145151
})

packages/@nodepack/cli/src/lib/ProjectCreateJob.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ module.exports = class ProjectCreateJob {
128128
// initial commit
129129
let gitCommitSuccess = true
130130
if (shouldInitGit) {
131-
gitCommitSuccess = await commitOnGit(cliOptions, isTestOrDebug)
131+
gitCommitSuccess = await commitOnGit(cliOptions, isTestOrDebug, `[nodepack] create project`)
132132
}
133133
stopSpinner()
134134

packages/@nodepack/maintenance/src/lib/Maintenance.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Maintenance {
100100
})
101101
const { migrations } = await migrator.prepare()
102102
if (migrations.length) {
103-
await this.shouldCommitState()
103+
await this.shouldCommitState(`[nodepack] before app migration`)
104104
log(`🚀 Migrating app code...`)
105105
const { migrationCount, allOptions } = await migrator.migrate(this.preset)
106106
log(`📝 ${migrationCount} app migration${migrationCount > 1 ? 's' : ''} applied!`)
@@ -110,6 +110,7 @@ class Maintenance {
110110

111111
// install additional deps (injected by migrations)
112112
await this.installDeps(`📦 Installing additional dependencies...`)
113+
await this.shouldCommitState(`[nodepack] after app migration`)
113114
}
114115

115116
// TODO Env Migrations
@@ -125,14 +126,15 @@ class Maintenance {
125126

126127
/**
127128
* Should be called each time the project is about to be modified.
129+
* @param {string} defaultMessage
128130
*/
129-
async shouldCommitState () {
131+
async shouldCommitState (defaultMessage) {
130132
if (this.preCommitAttempted || this.skipCommit) return
131133
// Commit app code before installing a new plugin
132134
// in case it modify files
133135
const shouldCommitState = await shouldUseGit(this.cwd, this.cliOptions)
134136
if (shouldCommitState) {
135-
const result = await commitOnGit(this.cliOptions, this.isTestOrDebug)
137+
const result = await commitOnGit(this.cliOptions, this.isTestOrDebug, defaultMessage)
136138
if (!result) {
137139
// Commit failed confirmation
138140
const answers = await inquirer.prompt([

packages/@nodepack/utils/src/git.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ const { hasGit, hasProjectGit } = require('./env')
33
/**
44
* @param {any} cliOptions
55
* @param {boolean} isTestOrDebug
6+
* @param {string} defaultMessage
67
* @returns {Promise.<boolean>} Git commit success
78
*/
8-
exports.commitOnGit = async function (cliOptions, isTestOrDebug) {
9+
exports.commitOnGit = async function (cliOptions, isTestOrDebug, defaultMessage) {
910
const { run } = this
1011
let success = true
1112
await run('git add -A')
1213
if (isTestOrDebug) {
1314
await run('git', ['config', 'user.name', 'test'])
1415
await run('git', ['config', 'user.email', 'test@test.com'])
1516
}
16-
const msg = typeof cliOptions.git === 'string' ? cliOptions.git : 'init'
17+
const msg = typeof cliOptions.git === 'string' ? cliOptions.git : defaultMessage
1718
try {
1819
await run('git', ['commit', '-m', msg])
1920
} catch (e) {

0 commit comments

Comments
 (0)