Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ jobs:

- name: Run tests
run: npm run test

- name: Smoke test the packed package
run: npm run test:package
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Follow these guidelines when modifying the repository.

- `npm run dev` – build in watch mode.
- `npm run typecheck` – run TypeScript type checking with `tsc --noEmit`.
- `npm run test:package` – after `npm run build`, pack the package, install it
in a temp project, and check the CLI and library work as published.

## License

Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ npm test

# Run tests in watch mode
npm run test:watch

# Pack, install and run the built package (run `npm run build` first)
npm run test:package
```

### Test Fixtures
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"prepublishOnly": "npm run build",
"test": "vitest run",
"test:watch": "vitest",
"test:all": "npm run lint && npm run typecheck && npm run build && npm run test",
"test:all": "npm run lint && npm run typecheck && npm run build && npm run test && npm run test:package",
"test:package": "node scripts/smoke-test-package.mjs",
"typecheck": "tsc --noEmit"
},
"dependencies": {
Expand Down
126 changes: 126 additions & 0 deletions scripts/smoke-test-package.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { execFileSync } from 'node:child_process';
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import assert from 'node:assert/strict';

const repoRoot = resolve(import.meta.dirname, '..');
const { version } = JSON.parse(
readFileSync(join(repoRoot, 'package.json'), 'utf-8'),
);
const allowedFiles = new Set([
'LICENSE',
'README.md',
'package.json',
'markdown-code.png',
]);
const npmCli = process.env.npm_execpath;

if (!npmCli) {
throw new Error('Run this through npm: npm run test:package');
}

if (!existsSync(join(repoRoot, 'dist', 'index.js'))) {
throw new Error('dist/ is missing. Run npm run build first.');
}

function run(command, args, cwd) {
return execFileSync(command, args, {
cwd,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim();
}

function npm(args, cwd) {
return run(process.execPath, [npmCli, ...args], cwd);
}

function mdCode(bin, args, cwd) {
return npm(['exec', '--no', '--', bin, ...args], cwd);
}

const workDir = mkdtempSync(join(tmpdir(), 'md-code-package-'));

try {
const [packed] = JSON.parse(
npm(['pack', '--json', '--pack-destination', workDir], repoRoot),
);
const unexpected = packed.files
.map((file) => file.path)
.filter((path) => !path.startsWith('dist/') && !allowedFiles.has(path));
assert.deepEqual(unexpected, [], 'unexpected files in the package');
assert.ok(
packed.files.some((file) => file.path === 'dist/index.d.ts'),
'package is missing dist/index.d.ts',
);

const project = join(workDir, 'project');
mkdirSync(join(project, 'snippets'), { recursive: true });
writeFileSync(
join(project, 'package.json'),
JSON.stringify({ name: 'smoke', version: '0.0.0', private: true }),
);
npm(
[
'install',
'--no-audit',
'--no-fund',
'--ignore-scripts',
join(workDir, packed.filename),
],
project,
);

for (const bin of ['md-code', 'markdown-code']) {
assert.equal(
mdCode(bin, ['--version'], project),
version,
`${bin} --version`,
);
}

writeFileSync(join(project, 'snippets', 'add.js'), 'const sum = 1 + 1;\n');
writeFileSync(
join(project, 'README.md'),
'# Smoke\n\n```js snippet=add.js\nstale\n```\n',
);
writeFileSync(
join(project, '.markdown-coderc.json'),
JSON.stringify({ snippetRoot: './snippets', markdownGlob: 'README.md' }),
);

mdCode('md-code', ['sync'], project);
assert.equal(
readFileSync(join(project, 'README.md'), 'utf-8'),
'# Smoke\n\n```js snippet=add.js\nconst sum = 1 + 1;\n```\n',
);
mdCode('md-code', ['check'], project);

const exported = run(
'node',
[
'--input-type=module',
'--eval',
"const m = await import('markdown-code'); console.log([typeof m.syncMarkdownFiles, m.VERSION].join(' '));",
],
project,
);
assert.equal(exported, `function ${version}`, 'library import');
assert.ok(
existsSync(
join(project, 'node_modules', 'markdown-code', 'dist', 'index.d.ts'),
),
);

console.log(`markdown-code@${version} package smoke test passed`);
} finally {
rmSync(workDir, { recursive: true, force: true });
}
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { VERSION } from './version.js';

yargs(hideBin(process.argv))
.scriptName('md-code')
Expand Down Expand Up @@ -38,7 +39,7 @@ yargs(hideBin(process.argv))
})
.help()
.alias('help', 'h')
.version()
.version(VERSION)
.alias('version', 'v')
.demandCommand(1, 'You need at least one command before moving on')
.parse();
Loading