Skip to content

Commit

Permalink
pear init improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed Jul 29, 2024
1 parent 7774f71 commit 4e92875
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ module.exports = async (ipc, argv = Bare.argv.slice(1)) => {
'init',
summary('Create initial project files'),
description('Template Types: desktop, terminal, terminal-node'),
arg('[link|type]', 'Template link or type to init from. Default: desktop'),
arg('<link|type=desktop>', 'Template link or type to init from.'),
arg('[dir]', 'Project directory path (default: .)'),
flag('--yes|-y', 'Autoselect all defaults'),
flag('--type|-t <type>', 'Project type: desktop (default) or terminal. Overrides: [link|type]'),
flag('--type|-t <type>', 'Template type. Overrides <link|type>'),
flag('--force|-f', 'Force overwrite existing files'),
flag('--with|-w [name]', 'Additional functionality. Available: node'),
runners.init(ipc)
Expand Down
28 changes: 12 additions & 16 deletions init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ async function init (link, dir, { ipc, header, autosubmit, defaults, force = fal
if (platform.key.startsWith('/')) link = path.join(__dirname, 'templates', link)
else link = 'pear://' + platform.key + '/init/templates/' + link
}

let params = null
if (isPear) {
const { drive } = parseLink(link)
Expand All @@ -28,6 +27,7 @@ async function init (link, dir, { ipc, header, autosubmit, defaults, force = fal
throw new ERR_PERMISSION_REQUIRED('Permission required to use template', drive.key)
}
}

for await (const { tag, data } of ipc.dump({ link: link + '/_template.json', dir: '-' })) {
if (tag !== 'file') continue
try {
Expand Down Expand Up @@ -56,22 +56,18 @@ async function init (link, dir, { ipc, header, autosubmit, defaults, force = fal
}

const prompt = new Interact(header, params, defaults)
try {
const locals = await prompt.run({ autosubmit })
for await (const { tag, data } of ipc.dump({ link, dir: '-' })) {
if (tag === 'error') {
throw new ERR_OPERATION_FAILED('Dump Failed: ' + data.stack)
}
if (tag !== 'file') continue
const { key, value = null } = data
if (key === '/_template.json') continue
if (value === null) continue // dir

const writeStream = dst.createWriteStream(transform.sync(key, locals))
await pipelinePromise(transform.stream(value, locals), writeStream)
const locals = await prompt.run({ autosubmit })
for await (const { tag, data } of ipc.dump({ link, dir: '-' })) {
if (tag === 'error') {
throw new ERR_OPERATION_FAILED('Dump Failed: ' + data.stack)
}
} finally {
await dst.close()
if (tag !== 'file') continue
const { key, value = null } = data
if (key === '/_template.json') continue
if (value === null) continue // dir

const writeStream = dst.createWriteStream(transform.sync(key, locals))
await pipelinePromise(transform.stream(value, locals), writeStream)
}
}

Expand Down
2 changes: 1 addition & 1 deletion init/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function parse (template, locals) {
const [match, def] = result
const [name] = def.split(':').map((s) => s.trim())
const { index } = result
args.push(locals[name])
args.push(locals[name] + '')
strings.push(template.slice(last, index))
last = index + match.length
}
Expand Down
13 changes: 5 additions & 8 deletions init/templates/desktop/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@
},
{
"name": "main",
"default": "index.js",
"default": "index.html",
"prompt": "main",
"type": "terminal",
"validation": "(value) => value.endsWith('.js')",
"msg": "must have a .js file extension"
"validation": "(value) => value.endsWith('.html')",
"msg": "must have an .html file extension"
},
{
"name": "height",
"validation": "(value) => Number.isInteger(+value)",
"prompt": "height",
"msg": "must be an integer",
"type": "desktop"
"msg": "must be an integer"
},
{
"name": "width",
"validation": "(value) => Number.isInteger(+value)",
"prompt": "width",
"msg": "must be an integer",
"type": "desktop"
"msg": "must be an integer"
},
{
"name": "license",
Expand Down
2 changes: 1 addition & 1 deletion init/templates/desktop/app.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
document.querySelector('h1').addEventListener('click', (e) => { e.target.innerHTML = '🍐' })
document.querySelector('h1').addEventListener('click', (e) => { e.target.innerHTML = '🍐' })
2 changes: 1 addition & 1 deletion init/templates/desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "__name__",
"main": "__main__",
"type": "module",
"pear": {
"name": "__name__",
"type": "desktop",
Expand All @@ -11,6 +10,7 @@
"width": __width__
}
},
"type": "module",
"license": "__license__",
"scripts": {
"dev": "pear run -d .",
Expand Down
1 change: 0 additions & 1 deletion init/templates/terminal-node/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"name": "main",
"default": "index.js",
"prompt": "main",
"type": "terminal",
"validation": "(value) => value.endsWith('.js')",
"msg": "must have a .js file extension"
},
Expand Down
1 change: 0 additions & 1 deletion init/templates/terminal/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"name": "main",
"default": "index.js",
"prompt": "main",
"type": "terminal",
"validation": "(value) => value.endsWith('.js')",
"msg": "must have a .js file extension"
},
Expand Down

0 comments on commit 4e92875

Please sign in to comment.