generated from niiknow/vue-wp-plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,650 additions
and
820 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env node | ||
// this is use to make deployment zip file | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const archiver = require('archiver') | ||
|
||
const IGNORE_LIST_FILENAME = '.gitattributes' | ||
const CURRENT_PATH = path.resolve(process.cwd()) | ||
|
||
const loadIgnoreFile = () => { | ||
try { | ||
const fileList = fs.readFileSync(CURRENT_PATH + `/${IGNORE_LIST_FILENAME}`).toString().split('\n') | ||
// .replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm, "") | ||
const result = [] | ||
fileList.forEach(f => { | ||
if (f.indexOf(' export-ignore') > 0) { | ||
const patterns = f.split(' ') | ||
result.push(patterns[0].trim()) | ||
} | ||
}) | ||
|
||
return result | ||
} catch (err) { | ||
return [] | ||
} | ||
} | ||
|
||
const archive = archiver('zip', { | ||
zlib: { | ||
level: 9 | ||
} | ||
}) | ||
const dirName = path.basename(CURRENT_PATH) | ||
const outFile = './' + dirName + '.zip' | ||
|
||
if (fs.existsSync(outFile)) { | ||
fs.unlinkSync(outFile) | ||
} | ||
console.time(`${outFile} zipped in`) | ||
|
||
const output = fs.createWriteStream(outFile) | ||
const ignoreList = loadIgnoreFile() | ||
ignoreList.push(outFile) | ||
|
||
// listen for all archive data to be written | ||
// 'close' event is fired only when a file descriptor is involved | ||
output.on('close', function() { | ||
console.log('>', (archive.pointer() / 1000000) + ' megabytes') | ||
}) | ||
|
||
// This event is fired when the data source is drained no matter what was the data source. | ||
// It is not part of this library but rather from the NodeJS Stream API. | ||
// @see: https://nodejs.org/api/stream.html#stream_event_end | ||
output.on('end', function() { | ||
console.log('Data has been drained') | ||
}) | ||
|
||
output.on('finish', () => console.timeEnd(`${outFile} zipped in`)) | ||
|
||
archive.on('entry', function (entry) { | ||
console.log('adding', entry.name) | ||
}) | ||
|
||
archive.on('warning', function (err) { | ||
if (err.code === 'ENOENT') { | ||
console.log('WARNING', err) | ||
} else { | ||
// throw error | ||
throw err | ||
} | ||
}) | ||
|
||
// good practice to catch this error explicitly | ||
archive.on('error', function(err) { | ||
throw err | ||
}) | ||
|
||
archive.pipe(output) | ||
|
||
archive.glob('**/*', | ||
{ | ||
expand: true, | ||
cwd: CURRENT_PATH, | ||
ignore: ignoreList, | ||
dot: true | ||
}, | ||
{ | ||
prefix: dirName | ||
}) | ||
|
||
archive.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#!/usr/bin/env node | ||
// init plugin | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const readline = require('node:readline/promises') | ||
const glob = require('glob') | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
const base_path = path.resolve(process.cwd()) | ||
const template_file = 'vue-wp-plugin-starter.php' | ||
|
||
const options = { | ||
PluginName: '', | ||
PluginSlug: '', | ||
PluginPrefix: '', | ||
PluginSpace: '', | ||
PluginFileName: '' | ||
} | ||
|
||
if (!fs.existsSync(template_file)) { | ||
rl.write(`!!! Unexpected error. Failed to locate template file ${template_file}`) | ||
process.exit(1) | ||
} | ||
|
||
const getPluginName = async () => { | ||
const answer = await rl.question('To begin, input your plugin name and press [ENTER]: ') | ||
|
||
if (!/^[A-Z][A-Za-z0-9]*( [A-Z][A-Za-z0-9]*)*$/.test(answer)) { | ||
rl.write(`Malformed name ${answer}. Please use title-case words separated by spaces. No hyphens. For example, 'Hello World'.`) | ||
return await getPluginName() | ||
} | ||
|
||
options.PluginName = answer | ||
} | ||
|
||
rl.on('close', async () => { | ||
const files = glob.sync('**/*.php', | ||
{ | ||
expand: true, | ||
dot: false, | ||
cwd: base_path, | ||
ignore: [ | ||
'.git/**', | ||
'assets/**', | ||
'bin/**', | ||
'node_modules/**', | ||
'public/**', | ||
'src/**', | ||
'vendor/**' | ||
] | ||
}) | ||
files.push('readme.txt') | ||
files.push('composer.json') | ||
files.push('package.json') | ||
files.push('tailwind.js') | ||
files.push('assets/admin.html') | ||
files.push('assets/frontend.html') | ||
files.push('assets/frontview.html') | ||
console.log('applying options: ', options) | ||
applyOptions(options, files) | ||
|
||
|
||
console.log('Removing template files') | ||
rename(template_file, `${PluginFileName}.php`) | ||
fs.unlinkSync('composer.lock') | ||
fs.unlinkSync('package-lock.json') | ||
process.exit(0) | ||
}) | ||
|
||
function applyOptions(options, files) { | ||
files.forEach((file) => { | ||
console.log('applying to file', file) | ||
let file_path = path.resolve(base_path, file) | ||
let data = fs.readFileSync(file_path, 'utf8') | ||
|
||
for (const [key, value] of Object.entries(options)) { | ||
// let find = '\\$\\{' + key + '\\}'; | ||
const find = key | ||
let re = new RegExp(find, 'g') | ||
|
||
data = data.replace(re, value) | ||
} | ||
|
||
fs.writeFileSync(file_path, data, 'utf8') | ||
}) | ||
} | ||
|
||
function rename(from_path, to_path) { | ||
fs.renameSync( | ||
path.resolve(base_path, from_path), | ||
path.resolve(base_path, to_path) | ||
) | ||
} | ||
|
||
function createSlug(str) { | ||
str = str.replace(/['\.]/g, '') | ||
return str.toLowerCase().split(' ').join('-') | ||
} | ||
|
||
function createPrefix(str) { | ||
str = str.replace(/['\.]/g, '') | ||
return str.toLowerCase().split(' ').join('_') | ||
} | ||
|
||
function createNamespace(str) { | ||
str = str.replace(/['\.]/g, '') | ||
return str.replace(/\s+/g, '') | ||
} | ||
|
||
(async () => { | ||
await getPluginName() | ||
|
||
options.PluginSlug = createSlug(options.PluginName) | ||
options.PluginPrefix = createPrefix(options.PluginName) | ||
options.PluginSpace = createNamespace(options.PluginName) | ||
options.PluginFileName = options.PluginSlug | ||
const prepend = await rl.question('Do you want to prepend \'wp-\' to your plugin file name? [y/n]: ') | ||
if (prepend.toLowerCase() === 'y') { | ||
options.PluginFileName = 'wp-' + options.PluginSlug | ||
} | ||
|
||
rl.close() | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.