-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at reworking injection/extraction API. (#160)
Implement CLI changes as per injection rework, add prettier build step, small testing framework.
- Loading branch information
1 parent
3a2e48b
commit 145840b
Showing
26 changed files
with
1,002 additions
and
663 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
{ | ||
"extends": "@antfu", | ||
"extends": [ | ||
"@antfu", | ||
"prettier" | ||
], | ||
"ignorePatterns": ["**/*importmap*"], | ||
"rules": { | ||
"no-console": "off", | ||
"no-cond-assign": "off" | ||
"no-cond-assign": "off", | ||
"antfu/if-newline": off | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
/sandbox | ||
/deno.lock | ||
/importmap.json | ||
index.html |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
version = 0.1 | ||
default-task = 'build' | ||
|
||
extensions = ['[email protected]:npm'] | ||
extensions = ['[email protected]:npm', '[email protected]:prettier'] | ||
|
||
[[task]] | ||
name = 'build' | ||
|
@@ -17,6 +17,14 @@ run = 'eslint .' | |
name = 'lint:fix' | ||
run = 'eslint . --fix' | ||
|
||
[[task]] | ||
name = 'prettier' | ||
template = 'prettier' | ||
deps = ['src/*.ts', 'test/*.ts'] | ||
[task.template-options] | ||
files = 'src/*.ts test/*.ts' | ||
loglevel = 'warn' | ||
|
||
[[task]] | ||
name = 'typecheck' | ||
deps = ['src/*.ts', 'test/*.ts'] | ||
|
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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
#!/usr/bin/env node | ||
import './dist/cli.js' | ||
import c from "picocolors"; | ||
import { cli, patchArgs } from './dist/cli.js' | ||
|
||
try { | ||
patchArgs(process.argv) | ||
cli.parse(); | ||
} catch (e) { | ||
if (e.constructor.name === "CACError") | ||
console.error(`${c.red("Err:")} ${e.message}\n`); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import c from 'picocolors' | ||
import { clearCache as _clearCache } from '@jspm/generator' | ||
import c from "picocolors"; | ||
import { clearCache as _clearCache } from "@jspm/generator"; | ||
|
||
export default async function clearCache() { | ||
_clearCache() | ||
console.warn(`${c.green('Ok:')} Cache cleared successfully`) | ||
_clearCache(); | ||
console.warn(`${c.green("Ok:")} Cache cleared successfully`); | ||
} |
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 |
---|---|---|
@@ -1,118 +1,120 @@ | ||
import c from 'picocolors' | ||
import cac from 'cac' | ||
import { version } from '../package.json' | ||
import clearCache from './clearCache' | ||
import extract from './extract' | ||
import inject from './inject' | ||
import install from './install' | ||
import link from './link' | ||
import uninstall from './uninstall' | ||
import update from './update' | ||
import { wrapCommandAndRemoveStack } from './utils' | ||
import c from "picocolors"; | ||
import cac from "cac"; | ||
import { version } from "../package.json"; | ||
import clearCache from "./clearCache"; | ||
import install from "./install"; | ||
import link from "./link"; | ||
import uninstall from "./uninstall"; | ||
import update from "./update"; | ||
import { wrapCommandAndRemoveStack } from "./utils"; | ||
|
||
const cli = cac(c.yellow('jspm')) | ||
export const cli = cac(c.yellow("jspm")); | ||
|
||
cli | ||
.usage('[command] [options]') | ||
.usage("[command] [options]") | ||
.version(version) | ||
.option('-r, --resolution <resolutions>', 'custom dependency resolution overrides for all installs') | ||
.option('-e, --env <environments>', 'the conditional environment resolutions to apply') | ||
.option('-m, --map <map>', 'an authoritative initial import map', { default: 'importmap.json' }) | ||
.option('-p, --provider <proider>', 'the default provider to use for a new install, defaults to `jspm`', { default: 'jspm' }) | ||
.option('--force', 'force install even if the import map is up to date', { default: false }) | ||
.option('--stdout', 'output the import map to stdout', { default: false }) | ||
.option('--compact', 'output a compact import map', { default: false }) | ||
.help() | ||
|
||
cli | ||
.command('install [...packages]', 'install packages') | ||
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map') | ||
.action(wrapCommandAndRemoveStack(install)) | ||
|
||
cli | ||
.command('update [...packages]', 'update packages') | ||
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map') | ||
.action(wrapCommandAndRemoveStack(update)) | ||
.option( | ||
"-m, --map <file>", | ||
"file to use as authoritative initial import map", | ||
{ default: "importmap.json" } | ||
) | ||
.option( | ||
"-e, --env <environments>", | ||
"the conditional environment resolutions to apply" | ||
) | ||
.option( | ||
"-p, --provider <provider>", | ||
"the default provider to use for a new install, defaults to `jspm`", | ||
{ default: "jspm" } | ||
) | ||
.option( | ||
"-r, --resolution <resolutions>", | ||
"custom dependency resolution overrides for all installs" | ||
) | ||
.option("--force", "force install even if the import map is up to date", { | ||
default: false, | ||
}) | ||
.option("--stdout", "output the import map to stdout", { default: false }) | ||
.option("--compact", "output a compact import map", { default: false }) | ||
.help(); | ||
|
||
cli | ||
.command('uninstall [...packages]', 'remove packages') | ||
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map') | ||
.action(wrapCommandAndRemoveStack(uninstall)) | ||
.command("install [...packages]", "install packages") | ||
.option("-o, --output <file>", "file to inject the final import map into") | ||
.action(wrapCommandAndRemoveStack(install)); | ||
|
||
|
||
cli | ||
.command('link [...modules]', 'trace install modules') | ||
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map') | ||
.action(wrapCommandAndRemoveStack(link)) | ||
.command("uninstall [...packages]", "remove packages") | ||
.option("-o, --output <file>", "file to inject the final import map into") | ||
.action(wrapCommandAndRemoveStack(uninstall)); | ||
|
||
cli | ||
.command('inject <htmlFile>', 'inject the import map into the given HTML file') | ||
.option('-p, --packages <...packages>', 'specific list of packages to extract for injection', { type: [] }) | ||
.option('--preload', 'preload the import map into the browser', { default: false }) | ||
.option('--integrity', 'generate integrity hashes for all dependencies', { default: false }) | ||
.option('-o, --output <outputFile>', '.html file for the output html with the import-map') | ||
.action(wrapCommandAndRemoveStack(inject)) | ||
.command("link [...modules]", "trace install modules") | ||
.alias("trace") | ||
.option("-o, --output <file>", "file to inject the final import map into") | ||
.action(wrapCommandAndRemoveStack(link)); | ||
|
||
cli | ||
.command('extract [...packages]', 'extract packages from the import map') | ||
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map') | ||
.action(wrapCommandAndRemoveStack(extract)) | ||
.command("update [...packages]", "update packages") | ||
.alias("upgrade") | ||
.option("-o, --output <file>", "file to inject the final import map into") | ||
.action(wrapCommandAndRemoveStack(update)); | ||
|
||
cli | ||
.command('clear-cache', 'Clear the local package cache') | ||
.action(wrapCommandAndRemoveStack(clearCache)) | ||
.command("clear-cache", "clear the local package cache") | ||
.action(wrapCommandAndRemoveStack(clearCache)); | ||
|
||
cli | ||
.command('') | ||
.action(() => { | ||
if (cli.args.length) | ||
console.error(`${c.red('Error:')} Invalid command ${c.bold(cli.args.join(' '))}\n`) | ||
else | ||
console.error(`${c.red('Error:')} No command provided\n`) | ||
cli.outputHelp() | ||
process.exit(1) | ||
}) | ||
// Help the user if they don't provide a command to run: | ||
cli.command("").action(() => { | ||
if (cli.args.length) | ||
console.error( | ||
`${c.red("Error:")} Invalid command ${c.bold(cli.args.join(" "))}\n` | ||
); | ||
else console.error(`${c.red("Error:")} No command provided\n`); | ||
cli.outputHelp(); | ||
process.exit(1); | ||
}); | ||
|
||
function noArgs() { | ||
if (cli.args.length === 0) { | ||
cli.outputHelp() | ||
process.exit(1) | ||
// Handler that ensures some commands always receive input: | ||
{ | ||
function noArgs() { | ||
if (cli.args.length === 0) { | ||
cli.outputHelp(); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
['uninstall', 'link', 'extract'].forEach(command => cli.on(`command:${command}`, noArgs)) | ||
["uninstall"].forEach((command) => cli.on(`command:${command}`, noArgs)); | ||
} | ||
|
||
// short commands and other hacks | ||
switch (process.argv[2]) { | ||
case 'cc': | ||
process.argv[2] = 'clear-cache' | ||
break | ||
case 'inject': { | ||
let pIndex = process.argv.indexOf('-p', 2) | ||
if (pIndex === -1) | ||
pIndex = process.argv.indexOf('--packages', 2) | ||
if (pIndex !== -1) { | ||
const pArgs = process.argv.splice(pIndex) | ||
for (let i = 0; i < pArgs.length; i++) { | ||
if (pArgs[i] === '-p' || pArgs[i] === '--packages') | ||
continue | ||
if (pArgs[i].startsWith('-')) { | ||
console.error(`${c.red('Err:')} --packages flag must be the last flag\n`) | ||
process.exit(1) | ||
} | ||
if (pArgs[i - 1] !== '-p' && pArgs[i - 1] !== '--packages') { | ||
pArgs.splice(i, 0, '-p') | ||
i++ | ||
// Hacks and small tweaks to the input arguments. Should be run before | ||
// calling cli.parse(). | ||
export function patchArgs(argv: string[]) { | ||
switch (argv[2]) { | ||
case "cc": | ||
argv[2] = "clear-cache"; | ||
break; | ||
case "inject": { | ||
let pIndex = argv.indexOf("-p", 2); | ||
if (pIndex === -1) pIndex = argv.indexOf("--packages", 2); | ||
if (pIndex !== -1) { | ||
const pArgs = argv.splice(pIndex); | ||
for (let i = 0; i < pArgs.length; i++) { | ||
if (pArgs[i] === "-p" || pArgs[i] === "--packages") continue; | ||
if (pArgs[i].startsWith("-")) { | ||
console.error( | ||
`${c.red("Err:")} --packages flag must be the last flag\n` | ||
); | ||
process.exit(1); | ||
} | ||
if (pArgs[i - 1] !== "-p" && pArgs[i - 1] !== "--packages") { | ||
pArgs.splice(i, 0, "-p"); | ||
i++; | ||
} | ||
} | ||
argv.splice(pIndex, pArgs.length, ...pArgs); | ||
} | ||
process.argv.splice(pIndex, pArgs.length, ...pArgs) | ||
} | ||
} | ||
} | ||
|
||
try { | ||
cli.parse() | ||
} | ||
catch (e) { | ||
if (e.constructor.name === 'CACError') | ||
console.error(`${c.red('Err:')} ${e.message}\n`) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.