Skip to content

Commit

Permalink
First pass at reworking injection/extraction API. (#160)
Browse files Browse the repository at this point in the history
Implement CLI changes as per injection rework, add prettier build step, small testing framework.
  • Loading branch information
Bubblyworld authored and guybedford committed Apr 8, 2023
1 parent b4f262f commit d701901
Show file tree
Hide file tree
Showing 26 changed files with 1,002 additions and 663 deletions.
8 changes: 6 additions & 2 deletions .eslintrc
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
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/sandbox
/deno.lock
/importmap.json
index.html
10 changes: 9 additions & 1 deletion chompfile.toml
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'
Expand All @@ -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']
Expand Down
11 changes: 10 additions & 1 deletion jspm.js
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`);
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"bin": {
"jspm": "./jspm.js"
},
"files": [
"dist",
"jspm.js"
],
"dependencies": {
"@jspm/generator": "^1.0.2",
"ora": "^6.1.2",
Expand All @@ -16,12 +20,10 @@
"cac": "^6.7.14",
"esbuild": "^0.16.10",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.6.0",
"prettier": "^2.8.4",
"tinyspy": "^1.0.2",
"tsx": "^3.12.1",
"typescript": "^4.9.4"
},
"files": [
"dist",
"jspm.js"
]
}
}
8 changes: 4 additions & 4 deletions src/clearCache.ts
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`);
}
190 changes: 96 additions & 94 deletions src/cli.ts
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`)
}
23 changes: 0 additions & 23 deletions src/extract.ts

This file was deleted.

Loading

0 comments on commit d701901

Please sign in to comment.