Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: link install test #129 (#140)
Browse files Browse the repository at this point in the history
Aslemammad authored and guybedford committed Apr 8, 2023
1 parent 49ae8ac commit aba5d51
Showing 10 changed files with 57 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -38,7 +38,12 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: true
- name: Setup Chomp
uses: guybedford/chomp-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.deno }}
- run: deno test --allow-env --allow-net --allow-read test/deno_test.ts
- run: chomp deno:test
10 changes: 10 additions & 0 deletions chompfile.toml
Original file line number Diff line number Diff line change
@@ -22,6 +22,16 @@ name = 'typecheck'
deps = ['src/*.ts', 'test/*.ts']
run = 'tsc --noEmit'

[[task]]
name = 'link:test'
dep = 'build'
run = 'node jspm.js link ./test/fixtures/test.js --env=deno,node'

[[task]]
name = 'deno:test'
dep = 'link:test'
run = 'deno --unstable run --importmap importmap.json -A ./test/fixtures/test.js'

[[task]]
name = 'test'
dep = 'test:'
8 changes: 4 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ Features like tracing a module and installing the dependencies, updating the mod

* Installing and Uninstalling npm dependencies
* Updating dependencies
* Trace Installing modules
* Linking (Trace installing) modules
* Injecting Import-maps
* Extracting packages from the Import-maps

@@ -46,13 +46,13 @@ Remove packages from the import map.
```sh
jspm uninstall react react-dom
```
### `trace-install`
> `jspm trace-install [...modules]`
### `link`
> `jspm link [...modules]`
Trace a module, installing all dependencies necessary into the map to support its execution including static and dynamic module imports.

```sh
jspm trace-install ./index.js
jspm link ./index.js
```
### `inject`
> `jspm inject <htmlFile> [...packages]`
8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { version } from '../package.json'
import extract from './extract'
import inject from './inject'
import install from './install'
import traceInstall from './traceInstall'
import link from './link'
import uninstall from './uninstall'
import update from './update'

@@ -37,9 +37,9 @@ cli
.action(uninstall)

cli
.command('trace-install [...modules]', 'trace install modules')
.command('link [...modules]', 'trace install modules')
.option('-o, --output <outputFile>', '.json or .importmap file for the output import-map')
.action(traceInstall)
.action(link)

cli
.command('inject <htmlFile> [...packages]', 'inject the import map into the provided HTML source')
@@ -62,7 +62,7 @@ function noArgs() {
}
}

['uninstall', 'trace-install', 'inject', 'extract'].forEach(command => cli.on(`command:${command}`, noArgs))
['uninstall', 'link', 'inject', 'extract'].forEach(command => cli.on(`command:${command}`, noArgs))

cli.on('command:*', () => {
console.error('Invalid command: %s', cli.args.join(' '))
6 changes: 5 additions & 1 deletion src/extract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Generator } from '@jspm/generator'
import type { Flags } from './types'
import { cwdUrl, getEnv, getInputMap, getInputMapUrl, getResolutions, startLoading, stopLoading, writeMap } from './utils'
import { cwdUrl, getEnv, getInputMap, getInputMapUrl, getResolutions, inputMapExists, startLoading, stopLoading, writeMap } from './utils'

export default async function extract(packages: string[], flags: Flags) {
if (!(await inputMapExists(flags))) {
console.error('No input map found, nothing to extract.')
return {}
}
const inputMap = await getInputMap(flags)
const env = getEnv(flags, true, inputMap)
startLoading(
7 changes: 6 additions & 1 deletion src/inject.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
getInputMap,
getInputMapUrl,
getResolutions,
inputMapExists,
startLoading,
stopLoading,
} from './utils'
@@ -35,9 +36,13 @@ export default async function inject(
htmlFile: string,
packages: string[],
flags: Flags,
) {
): Promise<string> {
const inputMap = await getInputMap(flags)
const env = getEnv(flags, true, inputMap)
if (!(await inputMapExists(flags))) {
console.error('No input map found, nothing to inject.')
return ''
}
if (!(await htmlExists(htmlFile))) {
console.warn(`Warning: HTML file ${htmlFile} does not exist, creating one`)
await fs.writeFile(htmlFile, defaultHtmlTemplate, 'utf-8')
21 changes: 13 additions & 8 deletions src/traceInstall.ts → src/link.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { Generator } from '@jspm/generator'
import type { Flags } from './types'
import { JspmError, getEnv, getInputMap, getResolutions, startLoading, stopLoading, writeMap, cwdUrl, getInputMapUrl } from './utils'
import { JspmError, cwdUrl, getEnv, getInputMap, getInputMapUrl, getResolutions, inputMapExists, startLoading, stopLoading, writeMap } from './utils'

export default async function traceInstall(packages: string[], flags: Flags) {
const resolvedPackages = packages.map((p) => {
export default async function link(packages: string[], flags: Flags) {
const resolvedModules = packages.map((p) => {
if (!p.includes('='))
return { target: p }
const [alias, target] = p.split('=')
return { alias, target }
})
if (!(await inputMapExists(flags))) {
console.error('No input map found, creating one.')
writeMap({}, flags, false, true)
}

const inputMap = await getInputMap(flags)
const env = getEnv(flags, true, inputMap)
startLoading(
`Tracing${
resolvedPackages.length
? ` ${resolvedPackages
`Linking${
resolvedModules.length
? ` ${resolvedModules
.map(p => p.alias || p.target)
.join(', ')}`
: ''
@@ -27,9 +32,9 @@ export default async function traceInstall(packages: string[], flags: Flags) {
mapUrl: getInputMapUrl(flags),
resolutions: getResolutions(flags),
})
if (!resolvedPackages.length)
if (!resolvedModules.length)
throw new JspmError('Trace install requires at least one module to trace.')
await generator.traceInstall(resolvedPackages.map(p => p.target))
await generator.traceInstall(resolvedModules.map(p => p.target))
stopLoading()
await writeMap(generator.getMap(), flags)
return generator.getMap()
5 changes: 4 additions & 1 deletion src/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Generator } from '@jspm/generator'
import type { Flags } from './types'
import { cwdUrl, getEnv, getInputMap, getInputMapUrl, startLoading, stopLoading, writeMap } from './utils'
import { cwdUrl, getEnv, getInputMap, getInputMapUrl, inputMapExists, startLoading, stopLoading, writeMap } from './utils'

export default async function uninstall(packages: string[], flags: Flags) {
if (!(await inputMapExists(flags)))
console.error('No input map found, nothing to uninstall.')

const inputMap = await getInputMap(flags)
const env = getEnv(flags, true, inputMap)
startLoading(`Uninstalling ${packages.join(', ')}`)
3 changes: 3 additions & 0 deletions test/fixtures/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import chalk from 'chalk'

console.log(chalk.bold('Chalk works'))
6 changes: 3 additions & 3 deletions test/traceInstall.test.ts → test/link.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import assert from 'assert'

import traceInstall from '../src/traceInstall'
import link from '../src/link'

{
/* basic traceInstall */
const map = await traceInstall(['./test/fixtures/a.js'], {
/* basic link */
const map = await link(['./test/fixtures/a.js'], {
stdout: true,
map: 'test/importmap.json',
})

0 comments on commit aba5d51

Please sign in to comment.