-
Notifications
You must be signed in to change notification settings - Fork 0
feat(scripts): scaffold a new Edge App via edge-apps-scripts create #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicomiguelino
wants to merge
10
commits into
main
Choose a base branch
from
feat/create-edge-app-scaffold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0161b8d
feat(scripts): scaffold a new Edge App via edge-apps-scripts create
nicomiguelino f614ef8
fix: address Copilot review feedback on create command
nicomiguelino 7fa1b4f
fix: reject unknown/malformed create args and escape --description sa…
nicomiguelino 5719c2c
fix: reject single-dash flags and preserve explicit empty --description
nicomiguelino 008d1a6
fix: show install step in next-steps when install fails, not just ski…
nicomiguelino a06f59f
docs: point the fallback error message at the public npx/bunx entrypo…
nicomiguelino 75a34f7
fix: reject scaffold-only options when no directory is given
nicomiguelino 44a4954
fix: re-read package.json after placeholder substitution before final…
nicomiguelino 2648f0f
fix: scaffold-generator cleanup, screenshot support, and CI regressio…
nicomiguelino fcfb04f
chore: bump version to 1.2.0
nicomiguelino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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.
This file contains hidden or 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 hidden or 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 hidden or 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,234 @@ | ||
| import fs from 'fs' | ||
| import path from 'path' | ||
| import { spawnSync } from 'child_process' | ||
| import { fileURLToPath } from 'url' | ||
| import { | ||
| toKebabCase, | ||
| toTitleCase, | ||
| walkTextFiles, | ||
| replaceInFile, | ||
| } from './template-utils.js' | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
| const libraryRoot = path.resolve(__dirname, '..') | ||
| const templateRoot = path.resolve(__dirname, 'create-template') | ||
| const libraryPkg = JSON.parse( | ||
| fs.readFileSync(path.join(libraryRoot, 'package.json'), 'utf-8'), | ||
| ) | ||
|
|
||
| const NPM_RUN_ALL2_VERSION = '^8.0.4' | ||
| const TYPES_BUN_VERSION = '^1.3.13' | ||
| const BUN_TYPES_VERSION = '^1.3.13' | ||
|
|
||
| const VALUE_FLAGS = new Set(['--pm', '--description', '--author']) | ||
| const BOOLEAN_FLAGS = new Set(['--force', '--skip-install']) | ||
|
|
||
| export function parseCreateArgs(args) { | ||
| const options = { | ||
| pm: null, | ||
| description: null, | ||
| author: null, | ||
| force: false, | ||
| skipInstall: false, | ||
| } | ||
| const positional = [] | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| const arg = args[i] | ||
| if (VALUE_FLAGS.has(arg)) { | ||
| const value = args[i + 1] | ||
| if (value === undefined || value.startsWith('-')) { | ||
| return { error: `Missing value for ${arg}` } | ||
| } | ||
| i++ | ||
| if (arg === '--pm') options.pm = value | ||
| else if (arg === '--description') options.description = value | ||
| else options.author = value | ||
| } else if (BOOLEAN_FLAGS.has(arg)) { | ||
| if (arg === '--force') options.force = true | ||
| else options.skipInstall = true | ||
| } else if (arg.startsWith('-')) { | ||
| return { error: `Unknown option: ${arg}` } | ||
| } else { | ||
| positional.push(arg) | ||
| } | ||
| } | ||
|
|
||
| if (positional.length > 1) { | ||
| return { | ||
| error: `Expected a single directory argument, got: ${positional.join(', ')}`, | ||
| } | ||
| } | ||
|
|
||
| return { directory: positional[0], options } | ||
| } | ||
|
|
||
| function validateDescription(description) { | ||
| if (description !== null && /[\r\n]/.test(description)) { | ||
| return 'Description cannot contain line breaks.' | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| function detectPackageManager(explicit) { | ||
| if (explicit) { | ||
| if (explicit !== 'npm' && explicit !== 'bun') { | ||
| console.error( | ||
| `Unsupported package manager: ${explicit}. Use "npm" or "bun".`, | ||
| ) | ||
| return null | ||
| } | ||
| return explicit | ||
| } | ||
|
|
||
| const userAgent = process.env.npm_config_user_agent || '' | ||
| return userAgent.startsWith('bun') ? 'bun' : 'npm' | ||
| } | ||
|
|
||
| function finalizePackageJson(destination, pm) { | ||
| const pkgPath = path.join(destination, 'package.json') | ||
| const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) | ||
|
|
||
| pkg.devDependencies = { | ||
| ...pkg.devDependencies, | ||
| '@screenly/edge-apps': `^${libraryPkg.version}`, | ||
| typescript: libraryPkg.dependencies.typescript, | ||
| prettier: libraryPkg.devDependencies.prettier, | ||
| '@types/node': libraryPkg.devDependencies['@types/node'], | ||
| 'npm-run-all2': NPM_RUN_ALL2_VERSION, | ||
| '@playwright/test': libraryPkg.peerDependencies['@playwright/test'], | ||
| } | ||
|
|
||
| if (pm === 'bun') { | ||
| pkg.scripts.test = 'bun test --pass-with-no-tests src/' | ||
| pkg.devDependencies['@types/bun'] = TYPES_BUN_VERSION | ||
| pkg.devDependencies['bun-types'] = BUN_TYPES_VERSION | ||
| pkg.devDependencies.jsdom = libraryPkg.dependencies.jsdom | ||
| pkg.devDependencies['@types/jsdom'] = | ||
| libraryPkg.devDependencies['@types/jsdom'] | ||
| } else { | ||
| pkg.scripts.test = 'vitest run --passWithNoTests' | ||
| pkg.devDependencies.vitest = libraryPkg.devDependencies.vitest | ||
| pkg.devDependencies.jsdom = libraryPkg.dependencies.jsdom | ||
| } | ||
| pkg.scripts['test:unit'] = pkg.scripts.test | ||
|
|
||
| fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8') | ||
| } | ||
|
|
||
| function installDependencies(destination, pm) { | ||
| console.log(`\nInstalling dependencies with ${pm}...`) | ||
| const result = spawnSync(pm, ['install'], { | ||
| cwd: destination, | ||
| stdio: 'inherit', | ||
| shell: process.platform === 'win32', | ||
| }) | ||
| if (result.status !== 0) { | ||
| console.warn( | ||
| `\n${pm} install failed. Run it manually inside ${destination}.`, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| function printScaffoldNextSteps(destination, appName, pm) { | ||
| const relativePath = path.relative(process.cwd(), destination) || '.' | ||
| const runCommand = pm === 'bun' ? 'bun run' : 'npm run' | ||
| const installCommand = pm === 'bun' ? 'bun install' : 'npm install' | ||
| const needsInstall = !fs.existsSync(path.join(destination, 'node_modules')) | ||
|
|
||
| const steps = [`cd "${relativePath}"`] | ||
| if (needsInstall) steps.push(installCommand) | ||
| steps.push( | ||
| `Register the app to get an id for screenly.yml:\n screenly edge-app create --name ${appName} --in-place`, | ||
| `Start the dev server:\n ${runCommand} dev`, | ||
| `Deploy when ready:\n ${runCommand} deploy`, | ||
| ) | ||
|
|
||
| console.log(` | ||
| Done! Your Edge App is ready. | ||
|
|
||
| Next steps: | ||
| ${steps.map((step, index) => ` ${index + 1}. ${step}`).join('\n')} | ||
| `) | ||
| } | ||
|
|
||
| export function scaffoldNewApp(directory, options) { | ||
| const pm = detectPackageManager(options.pm) | ||
| if (!pm) { | ||
| process.exitCode = 1 | ||
| return | ||
| } | ||
|
|
||
| const descriptionError = validateDescription(options.description) | ||
| if (descriptionError) { | ||
| console.error(descriptionError) | ||
| process.exitCode = 1 | ||
| return | ||
| } | ||
|
|
||
| const destination = path.resolve(process.cwd(), directory) | ||
|
|
||
| if (fs.existsSync(destination)) { | ||
| if (!fs.statSync(destination).isDirectory()) { | ||
| console.error(`"${directory}" already exists and is not a directory.`) | ||
| process.exitCode = 1 | ||
| return | ||
| } | ||
| const isEmpty = fs.readdirSync(destination).length === 0 | ||
| if (!isEmpty && !options.force) { | ||
| console.error( | ||
| `Directory "${directory}" already exists and is not empty. Use --force to write into it anyway.`, | ||
| ) | ||
| process.exitCode = 1 | ||
| return | ||
| } | ||
| } else { | ||
| fs.mkdirSync(destination, { recursive: true }) | ||
| } | ||
|
|
||
| const appName = toKebabCase(path.basename(destination)) | ||
| const appTitle = toTitleCase(appName) | ||
| const appDescription = | ||
| options.description ?? `${appTitle} - Screenly Edge App` | ||
|
|
||
| console.log(`\nScaffolding a new Edge App in ${destination}`) | ||
|
|
||
| fs.cpSync(templateRoot, destination, { recursive: true }) | ||
| fs.copyFileSync( | ||
| path.join(destination, '_gitignore'), | ||
| path.join(destination, '.gitignore'), | ||
| ) | ||
| fs.rmSync(path.join(destination, '_gitignore')) | ||
|
|
||
| const replacements = { | ||
| '{{APP_NAME}}': appName, | ||
| '{{APP_TITLE}}': appTitle, | ||
| '{{APP_DESCRIPTION}}': appDescription, | ||
| '{{APP_DESCRIPTION_JSON}}': JSON.stringify(appDescription).slice(1, -1), | ||
| '{{APP_DESCRIPTION_YAML}}': appDescription.replace(/'/g, "''"), | ||
| '{{PM_RUN}}': pm === 'bun' ? 'bun run' : 'npm run', | ||
| '{{PM_INSTALL}}': pm === 'bun' ? 'bun install' : 'npm install', | ||
| } | ||
| for (const filePath of walkTextFiles(destination)) { | ||
| replaceInFile(filePath, replacements) | ||
| } | ||
|
|
||
| finalizePackageJson(destination, pm) | ||
|
|
||
| if (options.author) { | ||
| const pkgPath = path.join(destination, 'package.json') | ||
| const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) | ||
| pkg.author = options.author | ||
| fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8') | ||
| } | ||
|
|
||
| if (pm === 'bun') { | ||
| fs.rmSync(path.join(destination, 'vitest.config.ts'), { force: true }) | ||
| } | ||
|
|
||
| if (!options.skipInstall) { | ||
| installDependencies(destination, pm) | ||
| } | ||
|
|
||
| printScaffoldNextSteps(destination, appName, pm) | ||
| } | ||
This file contains hidden or 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,6 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/prettierrc", | ||
| "semi": false, | ||
| "singleQuote": true, | ||
| "printWidth": 80 | ||
| } |
This file contains hidden or 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,43 @@ | ||
| # {{APP_TITLE}} | ||
|
|
||
| {{APP_DESCRIPTION}} | ||
|
|
||
| ## Getting Started | ||
|
|
||
| Install dependencies: | ||
|
|
||
| ```bash | ||
| {{PM_INSTALL}} | ||
| ``` | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| {{PM_RUN}} dev | ||
| ``` | ||
|
|
||
| ## Build | ||
|
|
||
| ```bash | ||
| {{PM_RUN}} build | ||
| ``` | ||
|
|
||
| ## Deployment | ||
|
|
||
| ```bash | ||
| screenly edge-app create --name {{APP_NAME}} --in-place | ||
| {{PM_RUN}} deploy | ||
| screenly edge-app instance create | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Setting | Description | Required | Default | | ||
| | --------- | ------------------------------- | -------- | ------------------ | | ||
| | `message` | The message displayed on screen | No | `Hello, Screenly!` | | ||
|
|
||
| ## Screenshots | ||
|
|
||
| ```bash | ||
| {{PM_RUN}} screenshots | ||
| ``` |
This file contains hidden or 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,5 @@ | ||
| node_modules/ | ||
| dist/ | ||
| *.log | ||
| .DS_Store | ||
| mock-data.yml |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.