generated from norskeld/serpent
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,781 additions
and
37 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { rollup, InputOptions, OutputOptions } from 'rollup' | ||
import typescript from '@rollup/plugin-typescript' | ||
import dts from 'rollup-plugin-dts' | ||
|
||
interface BundleOptions { | ||
input: InputOptions | ||
output: Array<OutputOptions> | ||
} | ||
|
||
function createBundleOptions(entry: string, destination: string): BundleOptions { | ||
return { | ||
input: { | ||
external: ['shiki', 'markdown-it'], | ||
input: `${entry}.ts`, | ||
plugins: [ | ||
typescript({ | ||
module: 'esnext', | ||
tsconfig: 'tsconfig.rollup.json' | ||
}) | ||
] | ||
}, | ||
output: [ | ||
{ | ||
file: `${destination}.cjs`, | ||
format: 'cjs', | ||
sourcemap: true, | ||
globals: { | ||
lodash: 'lodash' | ||
} | ||
}, | ||
{ | ||
file: `${destination}.mjs`, | ||
format: 'esm', | ||
sourcemap: true | ||
} | ||
] | ||
} | ||
} | ||
|
||
function createTypesBundleOptions(entry: string, destination: string): BundleOptions { | ||
return { | ||
input: { | ||
input: `${entry}.d.ts`, | ||
plugins: [dts()] | ||
}, | ||
output: [ | ||
{ | ||
file: `${destination}.d.ts`, | ||
format: 'esm' | ||
} | ||
] | ||
} | ||
} | ||
|
||
async function compile() { | ||
const options = [ | ||
// Roll-up sources. | ||
createBundleOptions('src/index', 'dist/index'), | ||
|
||
// Roll-up already existing d.ts created by `tsc`. | ||
createTypesBundleOptions('dist/types/index', 'dist/index') | ||
] | ||
|
||
try { | ||
await Promise.all( | ||
options.map(async ({ input, output }) => { | ||
const bundle = await rollup(input) | ||
|
||
for (const out of output) { | ||
await bundle.generate(out) | ||
await bundle.write(out) | ||
} | ||
|
||
await bundle.close() | ||
}) | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
compile() |
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { access, copyFile } from 'fs/promises' | ||
import { constants } from 'fs' | ||
import { resolve } from 'path' | ||
|
||
interface Success { | ||
kind: 'success' | ||
message: string | ||
} | ||
|
||
interface Failure { | ||
kind: 'failure' | ||
reason: string | ||
} | ||
|
||
type Result = Success | Failure | ||
type Actions = Record<string, () => Promise<Result>> | ||
|
||
async function actions(actions: Actions): Promise<Result> { | ||
const [passed] = process.argv.slice(2) | ||
const resolved = actions[passed] | ||
|
||
if (resolved) { | ||
return await resolved() | ||
} | ||
|
||
return { | ||
kind: 'failure', | ||
reason: `Action '${passed}' is not defined.` | ||
} | ||
} | ||
|
||
async function run(result: Result) { | ||
switch (result.kind) { | ||
case 'success': { | ||
console.log(result.message) | ||
return | ||
} | ||
|
||
case 'failure': { | ||
console.error(result.reason) | ||
process.exit(1) | ||
} | ||
} | ||
} | ||
|
||
async function main() { | ||
const cwd = process.cwd() | ||
|
||
await run( | ||
await actions({ | ||
/** This should be called in `prerelease`. */ | ||
async prepare() { | ||
try { | ||
// Just double-check for `dist` directory. | ||
await access(`${cwd}/package.json`, constants.F_OK) | ||
|
||
// Copy necessary files. | ||
await copyFile(`${cwd}/package.json`, `${cwd}/dist/package.json`) | ||
await copyFile(`${cwd}/CHANGELOG.md`, `${cwd}/dist/CHANGELOG.md`) | ||
await copyFile(`${cwd}/README.md`, `${cwd}/dist/README.md`) | ||
await copyFile(`${cwd}/LICENSE`, `${cwd}/dist/LICENSE`) | ||
|
||
// This is kinda needed for local publishes to `verdaccio`, so I do not accidentally | ||
// publish my experiments to the actual `npm` registry. | ||
await copyFile(`${cwd}/.npmrc`, `${cwd}/dist/.npmrc`) | ||
|
||
return { | ||
kind: 'success', | ||
message: 'Successfully prepared files for a release.' | ||
} | ||
} catch (error) { | ||
return { | ||
kind: 'failure', | ||
reason: (error as Error).message | ||
} | ||
} | ||
}, | ||
|
||
/** This should be called in `postversion`. Here we actually are inside the `dist`. */ | ||
async restore() { | ||
try { | ||
// Copy back. | ||
await copyFile(`${cwd}/package.json`, resolve(cwd, '..', 'package.json')) | ||
|
||
return { | ||
kind: 'success', | ||
message: 'Successfully restored `package.json`.' | ||
} | ||
} catch (error) { | ||
return { | ||
kind: 'failure', | ||
reason: (error as Error).message | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
|
||
main() |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type SetterFn = (value: string) => void | ||
type GetterFn = (separator?: string) => string | ||
|
||
type Builder = [getter: GetterFn, setter: SetterFn] | ||
|
||
/** Simple string builder. */ | ||
export function useBuilder(initial: Array<string> = []): Builder { | ||
const strings = Array.from(initial) | ||
|
||
const setterFn: SetterFn = (value: string) => strings.push(value) | ||
const getterFn: GetterFn = (separator = '') => strings.join(separator) | ||
|
||
return [getterFn, setterFn] | ||
} |
This file was deleted.
Oops, something went wrong.
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,3 +1,2 @@ | ||
export async function identity<T>(value: T): Promise<T> { | ||
return value | ||
} | ||
export type { ShikigamiOptions } from './shikigami.options' | ||
export { shikigami, loadTheme } from './shikigami' |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { merge } from './utils' | ||
|
||
export interface LineOption { | ||
/** Line number. **1-based**. */ | ||
line: number | ||
|
||
/** Line's classes list. */ | ||
classes: Array<string> | ||
} | ||
|
||
export type HtmlRendererDefaultOptions = Required<HtmlRendererOptions> | ||
|
||
export interface HtmlRendererOptions { | ||
/** | ||
* Set to `true` to render language specified in a fenced code block. | ||
* | ||
* @defaultValue false | ||
*/ | ||
withLanguage?: boolean | ||
|
||
/** | ||
* Set to `true` to render line numbers. | ||
* | ||
* @defaultValue false | ||
*/ | ||
withLineNumbers?: boolean | ||
|
||
/** | ||
* Use this to assign classes for specific lines. | ||
* | ||
* @defaultValue [] | ||
*/ | ||
lineOptions?: Array<LineOption> | ||
|
||
/** | ||
* Use this to change rendered language (if enabled). | ||
* | ||
* @defaultValue '' | ||
*/ | ||
languageId?: string | ||
|
||
/** | ||
* Use this to override text color for unstyled tokens. | ||
* | ||
* @defaultValue "#000" | ||
*/ | ||
foreground?: string | ||
|
||
/** | ||
* Use this to override background color for the whole `<pre>` block. | ||
* | ||
* @defaultValue "#fff" | ||
*/ | ||
background?: string | ||
} | ||
|
||
export function mergeOptions( | ||
defaults: HtmlRendererDefaultOptions, | ||
user: HtmlRendererOptions | ||
): HtmlRendererDefaultOptions { | ||
return merge(defaults, user) | ||
} | ||
|
||
export function getDefaultOptions(): HtmlRendererDefaultOptions { | ||
return { | ||
withLanguage: false, | ||
withLineNumbers: false, | ||
languageId: '', | ||
lineOptions: [], | ||
foreground: '#000', | ||
background: '#fff' | ||
} | ||
} |
Oops, something went wrong.