Skip to content

Commit 1b0592a

Browse files
committed
Use mpatch which was extracted from scripts/patch.js
1 parent 10440d7 commit 1b0592a

File tree

3 files changed

+4
-63
lines changed

3 files changed

+4
-63
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
}
8383
},
8484
"dependencies": {
85-
"glob": "*",
86-
"inquirer": "*"
85+
"inquirer": "*",
86+
"mpatch": "^0.1.0"
8787
}
8888
}

scripts/patch.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,7 @@
1-
const fs = require('fs')
2-
const glob = require('glob')
1+
const patch = require('mpatch')
32
const inquirer = require('inquirer')
43
const makeRecipes = require('./recipes')
54

6-
const debug = console.log
7-
8-
function checkPatch (text, patch) {
9-
return [text, patch]
10-
.map(x => x.replace(/\s+/g, ' '))
11-
.reduce((t, p) => t.includes(p))
12-
}
13-
14-
/**
15-
* @param {string} text
16-
* @param {Recipe} recipe
17-
* @return {?string}
18-
*/
19-
function applyRecipe (text, recipe) {
20-
if (!text) {
21-
return text
22-
}
23-
24-
if (checkPatch(text, recipe.patch)) {
25-
debug('already patched, skipped')
26-
return null
27-
}
28-
29-
const matched = text.match(recipe.pattern)
30-
31-
if (!matched) {
32-
debug(`not found ${recipe.pattern}`)
33-
return text
34-
}
35-
36-
return text.replace(matched[0], `${matched[0]}${recipe.patch}`)
37-
}
38-
39-
/**
40-
* @param {string} file
41-
* @param {Recipe[]} recipes
42-
*/
43-
function patch (file, recipes) {
44-
debug(`patching ${file}...`)
45-
46-
const path = glob.sync(file, {
47-
ignore: ['node_modules/**', '**/build/**']
48-
})[0]
49-
50-
const init = fs.readFileSync(path, 'utf8')
51-
const text = recipes.reduce(applyRecipe, init)
52-
53-
if (text != null) {
54-
fs.writeFileSync(path, text)
55-
}
56-
}
57-
585
const questions = [
596
{
607
type: 'input',
@@ -69,5 +16,5 @@ inquirer.prompt(questions).then(answers => {
6916
patch(file, [].concat(recipes[file]))
7017
})
7118

72-
debug('patch done!')
19+
console.log('done!')
7320
})

scripts/recipes.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* @typedef {object} Recipe
3-
* @param {string} pattern
4-
* @param {string} patch
5-
*/
6-
71
const defaultContext = {
82
appKey: '<Your JPush appKey>'
93
}

0 commit comments

Comments
 (0)