@fastly/create-compute
Advanced tools
Comparing version 0.1.0-beta.2 to 0.1.0-beta.3
import { type CommandLineOptions } from 'command-line-args'; | ||
export type ExecParams = { | ||
import { type Language } from './types.js'; | ||
export type CreateExecParams = { | ||
mode: 'create'; | ||
directory: string; | ||
@@ -7,2 +9,7 @@ authors: string[]; | ||
}; | ||
export type ListStarterKitsExecParams = { | ||
mode: 'list-starter-kits'; | ||
language?: Language; | ||
}; | ||
export type ExecParams = ListStarterKitsExecParams | CreateExecParams; | ||
export declare class BuildExecParamsCancelledError extends Error { | ||
@@ -9,0 +16,0 @@ messages: string[]; |
@@ -8,3 +8,3 @@ /* | ||
import { findReposStartWith, repoNameToPath } from './github.js'; | ||
import { KNOWN_STARTER_KITS, starterKitFullNameToShortName, starterKitShortNameToFullName, } from './fastlyStarterKits.js'; | ||
import { KNOWN_STARTER_KITS, defaultStarterKitForLanguage, starterKitFullNameToShortName, starterKitShortNameToFullName, } from './fastlyStarterKits.js'; | ||
const LANGUAGE_MAPPINGS = { | ||
@@ -24,2 +24,17 @@ 'js': 'javascript', | ||
export async function buildExecParams(commandLineOptions) { | ||
const listStarterKitsOptionValue = commandLineOptions['list-starter-kits']; | ||
if (Boolean(listStarterKitsOptionValue)) { | ||
let language = undefined; | ||
const languageOptionValue = commandLineOptions['language']; | ||
if (typeof languageOptionValue === 'string') { | ||
language = LANGUAGE_MAPPINGS[languageOptionValue]; | ||
if (language == null) { | ||
throw new BuildExecParamsCancelledError([`Unknown language value: ${languageOptionValue}`]); | ||
} | ||
} | ||
return { | ||
mode: 'list-starter-kits', | ||
language, | ||
}; | ||
} | ||
let directory; | ||
@@ -59,3 +74,3 @@ { | ||
} | ||
note(`Using directory: ${directory}.`); | ||
note(`Using directory: ${directory}`); | ||
// Whatever the value is, we will try to fs.mkdir() after confirmation to make sure | ||
@@ -74,3 +89,3 @@ // we can create the directory. | ||
if (authors.length > 0) { | ||
note(`Using specified authors: ${authors.join(', ')}.`); | ||
note(`Using specified authors: ${authors.join(', ')}`); | ||
} | ||
@@ -86,3 +101,3 @@ else { | ||
from = optionValue; | ||
note(`Using specified source path or URL: ${from}.`); | ||
note(`Using specified source path or URL: ${from}`); | ||
} | ||
@@ -98,3 +113,3 @@ else { | ||
language = LANGUAGE_MAPPINGS[optionValue]; | ||
note(`Using specified language: ${language}.`); | ||
note(`Using specified language: ${language}`); | ||
} | ||
@@ -166,80 +181,81 @@ else { | ||
else { | ||
const defaultStarterKit = defaultStarterKitForLanguage(language); | ||
let starterKit; | ||
{ | ||
const optionValue = commandLineOptions['default-starter-kit']; | ||
if (optionValue) { | ||
if (commandLineOptions['starter-kit'] != null) { | ||
const defaultStarterKitOptionValue = commandLineOptions['default-starter-kit']; | ||
const starterKitOptionValue = commandLineOptions['starter-kit']; | ||
if (defaultStarterKitOptionValue || starterKitOptionValue === 'default') { | ||
if (Boolean(defaultStarterKitOptionValue) && | ||
Boolean(starterKitOptionValue)) { | ||
throw new BuildExecParamsCancelledError([`'starter-kit' cannot be used with 'default-starter-kit'.`]); | ||
} | ||
starterKit = starterKitFullNameToShortName(language, KNOWN_STARTER_KITS[language][0].fullName); | ||
note(`Using default starter kit for '${language}'.`); | ||
from = repoNameToPath(defaultStarterKit.fullName); | ||
} | ||
else if (typeof starterKitOptionValue === 'string' && starterKitOptionValue !== '') { | ||
// We must allow any, because they might exist on GitHub. | ||
starterKit = starterKitOptionValue; | ||
note(`Using specified starter kit: ${starterKit}`); | ||
const fullName = starterKitShortNameToFullName(language, starterKit); | ||
from = repoNameToPath(fullName); | ||
} | ||
else { | ||
{ | ||
const optionValue = commandLineOptions['starter-kit']; | ||
if (typeof optionValue === 'string' && optionValue !== '') { | ||
// We must allow any, because they might exist on GitHub. | ||
starterKit = optionValue; | ||
note(`Using specified starter kit: ${starterKit}.`); | ||
} | ||
else { | ||
// Allow choosing from the known list first. | ||
// Allow choosing from the known list first. | ||
let promptValue = await select({ | ||
message: 'Select a starter kit', | ||
options: [ | ||
...KNOWN_STARTER_KITS[language].map(repository => { | ||
const shortName = starterKitFullNameToShortName(language, repository.fullName); | ||
return { | ||
value: repository.fullName, | ||
label: `[${shortName}] ${repository.description}`, | ||
}; | ||
}), | ||
{ | ||
const promptValue = await select({ | ||
message: 'Select a starter kit', | ||
options: [ | ||
...KNOWN_STARTER_KITS[language].map(repository => { | ||
const shortName = starterKitFullNameToShortName(language, repository.fullName); | ||
return { | ||
value: shortName, | ||
label: `[${shortName}] ${repository.description}`, | ||
}; | ||
}), | ||
{ | ||
value: '__other', | ||
label: 'Choose a starter kit from GitHub.', | ||
} | ||
], | ||
}); | ||
if (isCancel(promptValue)) { | ||
throw new BuildExecParamsCancelledError(); | ||
} | ||
if (promptValue === '__other') { | ||
let starterKits; | ||
const s = spinner(); | ||
try { | ||
s.start('Querying GitHub for starter kits...'); | ||
starterKits = await findReposStartWith(null, 'fastly', 'compute-starter-kit-' + language); | ||
} | ||
finally { | ||
s.stop(); | ||
} | ||
{ | ||
const promptValue = await select({ | ||
message: 'Select a starter kit', | ||
options: [ | ||
...starterKits.map(repository => { | ||
const shortName = starterKitFullNameToShortName(language, repository.fullName); | ||
return { | ||
value: shortName, | ||
label: `[${shortName}] ${repository.description}`, | ||
}; | ||
}), | ||
], | ||
}); | ||
if (isCancel(promptValue)) { | ||
throw new BuildExecParamsCancelledError(); | ||
} | ||
starterKit = promptValue; | ||
} | ||
} | ||
else { | ||
starterKit = promptValue; | ||
} | ||
value: '__other', | ||
label: 'Choose a starter kit from GitHub.', | ||
} | ||
], | ||
}); | ||
if (isCancel(promptValue)) { | ||
throw new BuildExecParamsCancelledError(); | ||
} | ||
if (promptValue === '__other') { | ||
// If other was chosen, then go to GitHub | ||
let starterKits; | ||
const s = spinner(); | ||
try { | ||
s.start('Querying GitHub for starter kits...'); | ||
starterKits = await findReposStartWith(null, 'fastly', 'compute-starter-kit-' + language); | ||
// Move "default" kit to front | ||
const defaultStarterKitIndex = starterKits.findIndex(kit => kit.fullName === defaultStarterKit.fullName); | ||
if (defaultStarterKitIndex !== -1) { | ||
starterKits = [ | ||
starterKits[defaultStarterKitIndex], | ||
...starterKits.slice(0, defaultStarterKitIndex), | ||
...starterKits.slice(defaultStarterKitIndex + 1), | ||
]; | ||
} | ||
} | ||
finally { | ||
s.stop(); | ||
} | ||
promptValue = await select({ | ||
message: 'Select a starter kit', | ||
options: [ | ||
...starterKits.map(repository => { | ||
const shortName = starterKitFullNameToShortName(language, repository.fullName); | ||
return { | ||
value: repository.fullName, | ||
label: `[${shortName}] ${repository.description}`, | ||
}; | ||
}), | ||
], | ||
}); | ||
if (isCancel(promptValue)) { | ||
throw new BuildExecParamsCancelledError(); | ||
} | ||
} | ||
from = repoNameToPath(promptValue); | ||
} | ||
const fullName = starterKitShortNameToFullName(language, starterKit); | ||
from = repoNameToPath(fullName); | ||
} | ||
@@ -250,2 +266,3 @@ } | ||
return { | ||
mode: 'create', | ||
directory, | ||
@@ -252,0 +269,0 @@ authors, |
@@ -1,4 +0,4 @@ | ||
import { type ExecParams } from './execParams.js'; | ||
import { type CreateExecParams } from './execParams.js'; | ||
export declare function getFastlyCliVersion(fastlyCli: string | null): string | null; | ||
export declare function execFastlyCli(fastlyCli: string | null, execParams: ExecParams): Promise<unknown>; | ||
export declare function execFastlyCli(fastlyCli: string | null, execParams: CreateExecParams): Promise<unknown>; | ||
//# sourceMappingURL=fastlyCommand.d.ts.map |
import { type Language, type Repository } from './types.js'; | ||
export declare const KNOWN_STARTER_KITS: Record<Language, Repository[]>; | ||
export declare function defaultStarterKitForLanguage(language: Language): Repository; | ||
export declare function starterKitFullNameToShortName(language: Language, fullName: string): string; | ||
export declare function starterKitShortNameToFullName(language: string, shortName: string): string; | ||
export declare function starterKitShortNameToFullName(language: Language, shortName: string): string; | ||
//# sourceMappingURL=fastlyStarterKits.d.ts.map |
@@ -23,3 +23,9 @@ /* | ||
}; | ||
export function defaultStarterKitForLanguage(language) { | ||
return KNOWN_STARTER_KITS[language][0]; | ||
} | ||
export function starterKitFullNameToShortName(language, fullName) { | ||
if (fullName === defaultStarterKitForLanguage(language).fullName) { | ||
return 'default'; | ||
} | ||
const prefix = `fastly/compute-starter-kit-${language}`; | ||
@@ -29,8 +35,10 @@ if (!fullName.startsWith(prefix)) { | ||
} | ||
return fullName.length > prefix.length ? fullName.slice(prefix.length + 1) : '(default)'; | ||
return fullName.slice(prefix.length + 1); | ||
} | ||
export function starterKitShortNameToFullName(language, shortName) { | ||
const prefix = `fastly/compute-starter-kit-${language}`; | ||
return shortName === '(default)' ? prefix : `${prefix}-${shortName}`; | ||
if (shortName === 'default') { | ||
return defaultStarterKitForLanguage(language).fullName; | ||
} | ||
return `fastly/compute-starter-kit-${language}-${shortName}`; | ||
} | ||
//# sourceMappingURL=fastlyStarterKits.js.map |
@@ -13,2 +13,4 @@ #!/usr/bin/env node | ||
import { getDirectoryStatus } from "./directory.js"; | ||
import { starterKitFullNameToShortName } from './fastlyStarterKits.js'; | ||
import { findReposStartWith } from './github.js'; | ||
const OPTION_DEFINITIONS = [ | ||
@@ -21,2 +23,3 @@ { name: 'help', type: Boolean, }, // Display help | ||
{ name: 'default-starter-kit', type: Boolean, }, // Default starter kit. Requires --language. | ||
{ name: 'list-starter-kits', type: Boolean, }, // List starter kits. | ||
{ name: 'from', type: String, }, // Path to a directory with a fastly.toml, a URL to a GitHub repo path with a fastly.toml, or a fiddle. | ||
@@ -47,4 +50,5 @@ { name: 'fastly-cli-path', type: String, }, // Path to the fastly CLI command. If not provided, then defaults to $(which fastly) (where in Windows) | ||
--default-starter-kit or --from. | ||
--default-starter-kit - Uses 'default' as the starter kit. Cannot be | ||
used with --starter-kit or --from. | ||
--default-starter-kit - Uses 'default' as the starter kit. | ||
Equivalent to --starter-kit=default. | ||
Cannot be used with --starter-kit or --from. | ||
--from=<pathspec-or-url> - Specifies a directory with a fastly.toml, a | ||
@@ -94,3 +98,3 @@ URL to a GitHub repo path with a fastly.toml, | ||
fastlyCliPath = optionValue; | ||
note(`Using specified fastly-cli-path: ${fastlyCliPath}.`); | ||
note(`Using specified fastly-cli-path: ${fastlyCliPath}`); | ||
} | ||
@@ -121,2 +125,39 @@ const fastlyCliVersion = getFastlyCliVersion(fastlyCliPath); | ||
} | ||
if (execParams.mode === 'list-starter-kits') { | ||
const starterKits = await findReposStartWith(null, 'fastly', 'compute-starter-kit'); | ||
let languages = [ | ||
'javascript', | ||
'typescript', | ||
]; | ||
if (execParams.language != null) { | ||
languages = [ | ||
execParams.language, | ||
]; | ||
} | ||
const languagesAndRepos = {}; | ||
for (const language of languages) { | ||
const prefix = `fastly/compute-starter-kit-${language}`; | ||
languagesAndRepos[language] = starterKits.filter(starterKitRepo => starterKitRepo.fullName.startsWith(prefix)).map(repository => { | ||
const { fullName, description } = repository; | ||
const shortName = starterKitFullNameToShortName(language, fullName); | ||
return { | ||
shortName, | ||
description, | ||
}; | ||
}); | ||
} | ||
const messages = []; | ||
messages.push('Available starter kits:'); | ||
messages.push(''); | ||
for (const [language, repos] of Object.entries(languagesAndRepos)) { | ||
messages.push(`Language: ${language}`); | ||
for (const repo of repos) { | ||
messages.push(` [${repo.shortName}] - ${repo.description}`); | ||
} | ||
} | ||
messages.push(''); | ||
messages.push('Use the value listed in brackets with the --starter-kit option.'); | ||
note(messages.join('\n')); | ||
process.exit(0); | ||
} | ||
let noConfirm = false; | ||
@@ -174,4 +215,4 @@ { | ||
} | ||
log.success(`Application created at ${appDirectory}.`); | ||
log.success(`Application created at: ${appDirectory}`); | ||
outro('Process completed!'); | ||
//# sourceMappingURL=index.js.map |
@@ -6,2 +6,6 @@ export type Language = 'javascript' | 'typescript'; | ||
}; | ||
export type RepoShort = { | ||
shortName: string; | ||
description: string; | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@fastly/create-compute", | ||
"version": "0.1.0-beta.2", | ||
"version": "0.1.0-beta.3", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A CLI for creating new JavaScript (TypeScript) applications on Fastly Compute", |
@@ -38,4 +38,7 @@ # @fastly/create-compute | ||
--default-starter-kit or --from. | ||
--default-starter-kit - Uses 'default' as the starter kit. Cannot be | ||
used with --starter-kit or --from. | ||
--default-starter-kit - Uses 'default' as the starter kit. | ||
Equivalent to --starter-kit=default. | ||
Cannot be used with --starter-kit or --from. | ||
--list-starter-kits - Query GitHub to list the starter kits | ||
available. May be used with --language. | ||
--from=<pathspec-or-url> - Specifies a directory with a fastly.toml, a | ||
@@ -42,0 +45,0 @@ URL to a GitHub repo path with a fastly.toml, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54872
701
86