initial commit
This commit is contained in:
21
node_modules/knitwork/LICENSE
generated
vendored
Normal file
21
node_modules/knitwork/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Pooya Parsa <pooya@pi0.io>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
141
node_modules/knitwork/README.md
generated
vendored
Normal file
141
node_modules/knitwork/README.md
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
# 🧶 knitwork
|
||||
|
||||
[![npm version][npm-version-src]][npm-version-href]
|
||||
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
||||
[![Github Actions][github-actions-src]][github-actions-href]
|
||||
[![Codecov][codecov-src]][codecov-href]
|
||||
|
||||
> Utilities to generate JavaScript code.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
# npm
|
||||
npm install knitwork
|
||||
|
||||
# yarn
|
||||
yarn add knitwork
|
||||
|
||||
# pnpm
|
||||
pnpm install knitwork
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
**Generating ESM syntax:**
|
||||
|
||||
```js
|
||||
import { genImport, genExport } from 'knitwork'
|
||||
|
||||
// import foo from "pkg"
|
||||
console.log(genImport('pkg', 'foo'))
|
||||
|
||||
// import { foo } from "pkg"
|
||||
console.log(genImport('pkg', ['foo']))
|
||||
|
||||
// import { a, b } from "pkg"
|
||||
console.log(genImport('pkg', ['a', 'b']))
|
||||
|
||||
// import foo as bar from "pkg";
|
||||
console.log(genImport('pkg', { name: 'foo', as: 'bar' }))
|
||||
|
||||
// import { foo as bar } from "pkg";
|
||||
console.log(genImport('pkg', [{ name: 'foo', as: 'bar' }]))
|
||||
|
||||
// import foo from "pkg" assert { type: "json" };
|
||||
console.log(genImport('pkg', 'foo', { assert: { type: 'json' } }))
|
||||
|
||||
// export foo from "pkg"
|
||||
console.log(genExport('pkg', 'foo'))
|
||||
|
||||
// export { a, b } from "pkg"
|
||||
console.log(genExport('pkg', ['a', 'b']))
|
||||
|
||||
// export * as bar from "pkg"
|
||||
console.log(genExport('pkg', { name: '*', as: 'bar' }))
|
||||
|
||||
// export foo from "pkg" assert { type: "json" };
|
||||
console.log(genExport('pkg', 'foo', { assert: { type: 'json' } }))
|
||||
```
|
||||
|
||||
**Generating TS:**
|
||||
|
||||
```js
|
||||
import { genInterface, genAugmentation, genInlineTypeImport, genTypeImport, genTypeExport } from 'knitwork'
|
||||
|
||||
// interface FooInterface extends A, B {
|
||||
// name: boolean
|
||||
// optional?: string
|
||||
// }
|
||||
console.log(genInterface('FooInterface', { name: 'boolean', 'optional?': 'string' }, { extends: ['A', 'B'] }))
|
||||
// declare module "my-module" {
|
||||
// interface MyInterface {}
|
||||
// }
|
||||
console.log(genAugmentation('my-module', { MyInterface: {} }))
|
||||
// typeof import("my-module").genString'
|
||||
console.log(genInlineTypeImport('my-module', 'genString'))
|
||||
// typeof import("my-module").default'
|
||||
console.log(genInlineTypeImport('my-module'))
|
||||
// import type { test as value } from "my-module";
|
||||
console.log(genTypeImport('my-module', [{ name: 'test', as: 'value' }]))
|
||||
// export type { test } from "my-module";
|
||||
console.log(genTypeExport('my-module', ['test']))
|
||||
```
|
||||
|
||||
**Serializing JS objects:**
|
||||
|
||||
```js
|
||||
import { genObjectFromRaw, genObjectFromRawEntries, genArrayFromRaw } from 'knitwork'
|
||||
|
||||
// { test: () => import("pkg") }
|
||||
console.log(genObjectFromRaw({ test: '() => import("pkg")' }))
|
||||
|
||||
// { 0: [ test, () => import("pkg") ] }
|
||||
console.log(genObjectFromRaw([ ['test', '() => import("pkg")'] ]))
|
||||
|
||||
const entries = Object.entries({
|
||||
a: 1, b: null, c: '"c"', nest: { hello: '"world"', fn: () => 1 }
|
||||
})
|
||||
// { a: 1, b: null, c: "c", nest: { hello: "world", fn: () => 1 } }
|
||||
console.log(genObjectFromRawEntries(entries))
|
||||
|
||||
// [ 1, 2, () => import("pkg") ]
|
||||
console.log(genArrayFromRaw(['1', '2', '() => import("pkg")']))
|
||||
```
|
||||
|
||||
**Generating safe variable names:**
|
||||
|
||||
```js
|
||||
import { genSafeVariableName } from 'knitwork'
|
||||
|
||||
// _123_32foo
|
||||
genSafeVariableName('123 foo')
|
||||
// _for
|
||||
genSafeVariableName('for')
|
||||
```
|
||||
|
||||
## 💻 Development
|
||||
|
||||
- Clone this repository
|
||||
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
|
||||
- Install dependencies using `pnpm install`
|
||||
- Run interactive tests using `pnpm dev`
|
||||
|
||||
## License
|
||||
|
||||
Made with 💛
|
||||
|
||||
Published under [MIT License](./LICENSE).
|
||||
|
||||
<!-- Badges -->
|
||||
[npm-version-src]: https://img.shields.io/npm/v/knitwork?style=flat-square
|
||||
[npm-version-href]: https://npmjs.com/package/knitwork
|
||||
|
||||
[npm-downloads-src]: https://img.shields.io/npm/dm/knitwork?style=flat-square
|
||||
[npm-downloads-href]: https://npmjs.com/package/knitwork
|
||||
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/knitwork/ci/main?style=flat-square
|
||||
[github-actions-href]: https://github.com/unjs/knitwork/actions?query=workflow%3Aci
|
||||
|
||||
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/knitwork/main?style=flat-square
|
||||
[codecov-href]: https://codecov.io/gh/unjs/knitwork
|
||||
217
node_modules/knitwork/dist/index.cjs
generated
vendored
Normal file
217
node_modules/knitwork/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
'use strict';
|
||||
|
||||
function genString(input, options = {}) {
|
||||
const string_ = JSON.stringify(input);
|
||||
if (!options.singleQuotes) {
|
||||
return JSON.stringify(input);
|
||||
}
|
||||
return `'${escapeString(string_)}'`;
|
||||
}
|
||||
const NEEDS_ESCAPE_RE = /[\n\r'\\\u2028\u2029]/;
|
||||
const QUOTE_NEWLINE_RE = /([\n\r'\u2028\u2029])/g;
|
||||
const BACKSLASH_RE = /\\/g;
|
||||
function escapeString(id) {
|
||||
if (!NEEDS_ESCAPE_RE.test(id)) {
|
||||
return id;
|
||||
}
|
||||
return id.replace(BACKSLASH_RE, "\\\\").replace(QUOTE_NEWLINE_RE, "\\$1");
|
||||
}
|
||||
|
||||
function genImport(specifier, imports, options = {}) {
|
||||
return _genStatement("import", specifier, imports, options);
|
||||
}
|
||||
function genTypeImport(specifier, imports, options = {}) {
|
||||
return _genStatement("import type", specifier, imports, options);
|
||||
}
|
||||
function genTypeExport(specifier, imports, options = {}) {
|
||||
return _genStatement("export type", specifier, imports, options);
|
||||
}
|
||||
const genInlineTypeImport = (specifier, name = "default", options = {}) => {
|
||||
return `typeof ${genDynamicImport(specifier, { ...options, wrapper: false })}.${name}`;
|
||||
};
|
||||
function genExport(specifier, exports, options = {}) {
|
||||
return _genStatement("export", specifier, exports, options);
|
||||
}
|
||||
function _genStatement(type, specifier, names, options = {}) {
|
||||
const specifierString = genString(specifier, options);
|
||||
if (!names) {
|
||||
return `${type} ${specifierString};`;
|
||||
}
|
||||
const nameArray = Array.isArray(names);
|
||||
const _names = (nameArray ? names : [names]).map((index) => {
|
||||
if (typeof index === "string") {
|
||||
return { name: index };
|
||||
}
|
||||
if (index.name === index.as) {
|
||||
index = { name: index.name };
|
||||
}
|
||||
return index;
|
||||
});
|
||||
const namesString = _names.map((index) => index.as ? `${index.name} as ${index.as}` : index.name).join(", ");
|
||||
if (nameArray) {
|
||||
return `${type} { ${namesString} } from ${genString(specifier, options)}${_genAssertClause(type, options.assert)};`;
|
||||
}
|
||||
return `${type} ${namesString} from ${genString(specifier, options)}${_genAssertClause(type, options.assert)};`;
|
||||
}
|
||||
function _genAssertClause(type, assert) {
|
||||
if (type === "import type" || type === "export type") {
|
||||
return "";
|
||||
}
|
||||
if (!assert || typeof assert !== "object") {
|
||||
return "";
|
||||
}
|
||||
return ` assert { type: ${genString(assert.type)} }`;
|
||||
}
|
||||
function genDynamicImport(specifier, options = {}) {
|
||||
const commentString = options.comment ? ` /* ${options.comment} */` : "";
|
||||
const wrapperString = options.wrapper === false ? "" : "() => ";
|
||||
const ineropString = options.interopDefault ? ".then(m => m.default || m)" : "";
|
||||
const optionsString = _genDynamicImportOptions(options);
|
||||
return `${wrapperString}import(${genString(specifier, options)}${commentString}${optionsString})${ineropString}`;
|
||||
}
|
||||
function _genDynamicImportOptions(options = {}) {
|
||||
return options.assert && typeof options.assert === "object" ? `, { assert: { type: ${genString(options.assert.type)} } }` : "";
|
||||
}
|
||||
function genSafeVariableName(name) {
|
||||
if (reservedNames.has(name)) {
|
||||
return `_${name}`;
|
||||
}
|
||||
return name.replace(/^\d/, (r) => `_${r}`).replace(/\W/g, (r) => "_" + r.charCodeAt(0));
|
||||
}
|
||||
const reservedNames = /* @__PURE__ */ new Set([
|
||||
"Infinity",
|
||||
"NaN",
|
||||
"arguments",
|
||||
"await",
|
||||
"break",
|
||||
"case",
|
||||
"catch",
|
||||
"class",
|
||||
"const",
|
||||
"continue",
|
||||
"debugger",
|
||||
"default",
|
||||
"delete",
|
||||
"do",
|
||||
"else",
|
||||
"enum",
|
||||
"eval",
|
||||
"export",
|
||||
"extends",
|
||||
"false",
|
||||
"finally",
|
||||
"for",
|
||||
"function",
|
||||
"if",
|
||||
"implements",
|
||||
"import",
|
||||
"in",
|
||||
"instanceof",
|
||||
"interface",
|
||||
"let",
|
||||
"new",
|
||||
"null",
|
||||
"package",
|
||||
"private",
|
||||
"protected",
|
||||
"public",
|
||||
"return",
|
||||
"static",
|
||||
"super",
|
||||
"switch",
|
||||
"this",
|
||||
"throw",
|
||||
"true",
|
||||
"try",
|
||||
"typeof",
|
||||
"undefined",
|
||||
"var",
|
||||
"void",
|
||||
"while",
|
||||
"with",
|
||||
"yield"
|
||||
]);
|
||||
|
||||
function wrapInDelimiters(lines, indent = "", delimiters = "{}", withComma = true) {
|
||||
if (lines.length === 0) {
|
||||
return delimiters;
|
||||
}
|
||||
const [start, end] = delimiters;
|
||||
return `${start}
|
||||
` + lines.join(withComma ? ",\n" : "\n") + `
|
||||
${indent}${end}`;
|
||||
}
|
||||
const VALID_IDENTIFIER_RE = /^[$_]?\w*$/;
|
||||
function genObjectKey(key) {
|
||||
return VALID_IDENTIFIER_RE.test(key) ? key : genString(key);
|
||||
}
|
||||
|
||||
function genObjectFromRaw(object, indent = "") {
|
||||
return genObjectFromRawEntries(Object.entries(object), indent);
|
||||
}
|
||||
function genArrayFromRaw(array, indent = "") {
|
||||
const newIdent = indent + " ";
|
||||
return wrapInDelimiters(array.map((index) => `${newIdent}${genRawValue(index, newIdent)}`), indent, "[]");
|
||||
}
|
||||
function genObjectFromRawEntries(array, indent = "") {
|
||||
const newIdent = indent + " ";
|
||||
return wrapInDelimiters(array.map(([key, value]) => `${newIdent}${genObjectKey(key)}: ${genRawValue(value, newIdent)}`), indent, "{}");
|
||||
}
|
||||
function genRawValue(value, indent = "") {
|
||||
if (typeof value === "undefined") {
|
||||
return "undefined";
|
||||
}
|
||||
if (value === null) {
|
||||
return "null";
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return genArrayFromRaw(value, indent);
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
return genObjectFromRaw(value, indent);
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
const genTypeObject = (object, indent = "") => {
|
||||
const newIndent = indent + " ";
|
||||
return wrapInDelimiters(Object.entries(object).map(([key, value]) => {
|
||||
const [, k = key, optional = ""] = key.match(/^(.*[^?])(\?)?$/) || [];
|
||||
if (typeof value === "string") {
|
||||
return `${newIndent}${genObjectKey(k)}${optional}: ${value}`;
|
||||
}
|
||||
return `${newIndent}${genObjectKey(k)}${optional}: ${genTypeObject(value, newIndent)}`;
|
||||
}), indent, "{}", false);
|
||||
};
|
||||
const genInterface = (name, contents, options = {}) => {
|
||||
const result = [
|
||||
options.export && "export",
|
||||
`interface ${name}`,
|
||||
options.extends && `extends ${Array.isArray(options.extends) ? options.extends.join(", ") : options.extends}`,
|
||||
contents ? genTypeObject(contents) : "{}"
|
||||
].filter(Boolean).join(" ");
|
||||
return result;
|
||||
};
|
||||
const genAugmentation = (specifier, interfaces) => {
|
||||
return `declare module ${genString(specifier)} ${wrapInDelimiters(
|
||||
Object.entries(interfaces || {}).map(
|
||||
([key, entry]) => " " + (Array.isArray(entry) ? genInterface(key, ...entry) : genInterface(key, entry))
|
||||
)
|
||||
)}`;
|
||||
};
|
||||
|
||||
exports.escapeString = escapeString;
|
||||
exports.genArrayFromRaw = genArrayFromRaw;
|
||||
exports.genAugmentation = genAugmentation;
|
||||
exports.genDynamicImport = genDynamicImport;
|
||||
exports.genExport = genExport;
|
||||
exports.genImport = genImport;
|
||||
exports.genInlineTypeImport = genInlineTypeImport;
|
||||
exports.genInterface = genInterface;
|
||||
exports.genObjectFromRaw = genObjectFromRaw;
|
||||
exports.genObjectFromRawEntries = genObjectFromRawEntries;
|
||||
exports.genSafeVariableName = genSafeVariableName;
|
||||
exports.genString = genString;
|
||||
exports.genTypeExport = genTypeExport;
|
||||
exports.genTypeImport = genTypeImport;
|
||||
exports.genTypeObject = genTypeObject;
|
||||
50
node_modules/knitwork/dist/index.d.ts
generated
vendored
Normal file
50
node_modules/knitwork/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
interface CodegenOptions {
|
||||
singleQuotes?: Boolean;
|
||||
assert?: {
|
||||
type: string;
|
||||
};
|
||||
}
|
||||
|
||||
declare type ESMImport = string | {
|
||||
name: string;
|
||||
as?: string;
|
||||
};
|
||||
declare function genImport(specifier: string, imports?: ESMImport | ESMImport[], options?: CodegenOptions): string;
|
||||
declare function genTypeImport(specifier: string, imports: ESMImport[], options?: CodegenOptions): string;
|
||||
declare function genTypeExport(specifier: string, imports: ESMImport[], options?: CodegenOptions): string;
|
||||
declare const genInlineTypeImport: (specifier: string, name?: string, options?: CodegenOptions) => string;
|
||||
declare type ESMExport = string | {
|
||||
name: string;
|
||||
as?: string;
|
||||
};
|
||||
declare function genExport(specifier: string, exports?: ESMExport | ESMExport[], options?: CodegenOptions): string;
|
||||
interface DynamicImportOptions extends CodegenOptions {
|
||||
comment?: string;
|
||||
wrapper?: boolean;
|
||||
interopDefault?: boolean;
|
||||
assert?: {
|
||||
type: string;
|
||||
};
|
||||
}
|
||||
declare function genDynamicImport(specifier: string, options?: DynamicImportOptions): string;
|
||||
declare function genSafeVariableName(name: string): string;
|
||||
|
||||
declare function genObjectFromRaw(object: Record<string, any>, indent?: string): string;
|
||||
declare function genArrayFromRaw(array: any[], indent?: string): string;
|
||||
declare function genObjectFromRawEntries(array: [key: string, value: any][], indent?: string): string;
|
||||
|
||||
declare function genString(input: string, options?: CodegenOptions): string;
|
||||
declare function escapeString(id: string): string;
|
||||
|
||||
declare type TypeObject = {
|
||||
[key: string]: string | TypeObject;
|
||||
};
|
||||
interface GenInterfaceOptions {
|
||||
extends?: string | string[];
|
||||
export?: boolean;
|
||||
}
|
||||
declare const genTypeObject: (object: TypeObject, indent?: string) => any;
|
||||
declare const genInterface: (name: string, contents?: TypeObject, options?: GenInterfaceOptions) => string;
|
||||
declare const genAugmentation: (specifier: string, interfaces?: Record<string, TypeObject | [TypeObject, Omit<GenInterfaceOptions, "export">]>) => string;
|
||||
|
||||
export { CodegenOptions, DynamicImportOptions, ESMExport, ESMImport, GenInterfaceOptions, TypeObject, escapeString, genArrayFromRaw, genAugmentation, genDynamicImport, genExport, genImport, genInlineTypeImport, genInterface, genObjectFromRaw, genObjectFromRawEntries, genSafeVariableName, genString, genTypeExport, genTypeImport, genTypeObject };
|
||||
201
node_modules/knitwork/dist/index.mjs
generated
vendored
Normal file
201
node_modules/knitwork/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
function genString(input, options = {}) {
|
||||
const string_ = JSON.stringify(input);
|
||||
if (!options.singleQuotes) {
|
||||
return JSON.stringify(input);
|
||||
}
|
||||
return `'${escapeString(string_)}'`;
|
||||
}
|
||||
const NEEDS_ESCAPE_RE = /[\n\r'\\\u2028\u2029]/;
|
||||
const QUOTE_NEWLINE_RE = /([\n\r'\u2028\u2029])/g;
|
||||
const BACKSLASH_RE = /\\/g;
|
||||
function escapeString(id) {
|
||||
if (!NEEDS_ESCAPE_RE.test(id)) {
|
||||
return id;
|
||||
}
|
||||
return id.replace(BACKSLASH_RE, "\\\\").replace(QUOTE_NEWLINE_RE, "\\$1");
|
||||
}
|
||||
|
||||
function genImport(specifier, imports, options = {}) {
|
||||
return _genStatement("import", specifier, imports, options);
|
||||
}
|
||||
function genTypeImport(specifier, imports, options = {}) {
|
||||
return _genStatement("import type", specifier, imports, options);
|
||||
}
|
||||
function genTypeExport(specifier, imports, options = {}) {
|
||||
return _genStatement("export type", specifier, imports, options);
|
||||
}
|
||||
const genInlineTypeImport = (specifier, name = "default", options = {}) => {
|
||||
return `typeof ${genDynamicImport(specifier, { ...options, wrapper: false })}.${name}`;
|
||||
};
|
||||
function genExport(specifier, exports, options = {}) {
|
||||
return _genStatement("export", specifier, exports, options);
|
||||
}
|
||||
function _genStatement(type, specifier, names, options = {}) {
|
||||
const specifierString = genString(specifier, options);
|
||||
if (!names) {
|
||||
return `${type} ${specifierString};`;
|
||||
}
|
||||
const nameArray = Array.isArray(names);
|
||||
const _names = (nameArray ? names : [names]).map((index) => {
|
||||
if (typeof index === "string") {
|
||||
return { name: index };
|
||||
}
|
||||
if (index.name === index.as) {
|
||||
index = { name: index.name };
|
||||
}
|
||||
return index;
|
||||
});
|
||||
const namesString = _names.map((index) => index.as ? `${index.name} as ${index.as}` : index.name).join(", ");
|
||||
if (nameArray) {
|
||||
return `${type} { ${namesString} } from ${genString(specifier, options)}${_genAssertClause(type, options.assert)};`;
|
||||
}
|
||||
return `${type} ${namesString} from ${genString(specifier, options)}${_genAssertClause(type, options.assert)};`;
|
||||
}
|
||||
function _genAssertClause(type, assert) {
|
||||
if (type === "import type" || type === "export type") {
|
||||
return "";
|
||||
}
|
||||
if (!assert || typeof assert !== "object") {
|
||||
return "";
|
||||
}
|
||||
return ` assert { type: ${genString(assert.type)} }`;
|
||||
}
|
||||
function genDynamicImport(specifier, options = {}) {
|
||||
const commentString = options.comment ? ` /* ${options.comment} */` : "";
|
||||
const wrapperString = options.wrapper === false ? "" : "() => ";
|
||||
const ineropString = options.interopDefault ? ".then(m => m.default || m)" : "";
|
||||
const optionsString = _genDynamicImportOptions(options);
|
||||
return `${wrapperString}import(${genString(specifier, options)}${commentString}${optionsString})${ineropString}`;
|
||||
}
|
||||
function _genDynamicImportOptions(options = {}) {
|
||||
return options.assert && typeof options.assert === "object" ? `, { assert: { type: ${genString(options.assert.type)} } }` : "";
|
||||
}
|
||||
function genSafeVariableName(name) {
|
||||
if (reservedNames.has(name)) {
|
||||
return `_${name}`;
|
||||
}
|
||||
return name.replace(/^\d/, (r) => `_${r}`).replace(/\W/g, (r) => "_" + r.charCodeAt(0));
|
||||
}
|
||||
const reservedNames = /* @__PURE__ */ new Set([
|
||||
"Infinity",
|
||||
"NaN",
|
||||
"arguments",
|
||||
"await",
|
||||
"break",
|
||||
"case",
|
||||
"catch",
|
||||
"class",
|
||||
"const",
|
||||
"continue",
|
||||
"debugger",
|
||||
"default",
|
||||
"delete",
|
||||
"do",
|
||||
"else",
|
||||
"enum",
|
||||
"eval",
|
||||
"export",
|
||||
"extends",
|
||||
"false",
|
||||
"finally",
|
||||
"for",
|
||||
"function",
|
||||
"if",
|
||||
"implements",
|
||||
"import",
|
||||
"in",
|
||||
"instanceof",
|
||||
"interface",
|
||||
"let",
|
||||
"new",
|
||||
"null",
|
||||
"package",
|
||||
"private",
|
||||
"protected",
|
||||
"public",
|
||||
"return",
|
||||
"static",
|
||||
"super",
|
||||
"switch",
|
||||
"this",
|
||||
"throw",
|
||||
"true",
|
||||
"try",
|
||||
"typeof",
|
||||
"undefined",
|
||||
"var",
|
||||
"void",
|
||||
"while",
|
||||
"with",
|
||||
"yield"
|
||||
]);
|
||||
|
||||
function wrapInDelimiters(lines, indent = "", delimiters = "{}", withComma = true) {
|
||||
if (lines.length === 0) {
|
||||
return delimiters;
|
||||
}
|
||||
const [start, end] = delimiters;
|
||||
return `${start}
|
||||
` + lines.join(withComma ? ",\n" : "\n") + `
|
||||
${indent}${end}`;
|
||||
}
|
||||
const VALID_IDENTIFIER_RE = /^[$_]?\w*$/;
|
||||
function genObjectKey(key) {
|
||||
return VALID_IDENTIFIER_RE.test(key) ? key : genString(key);
|
||||
}
|
||||
|
||||
function genObjectFromRaw(object, indent = "") {
|
||||
return genObjectFromRawEntries(Object.entries(object), indent);
|
||||
}
|
||||
function genArrayFromRaw(array, indent = "") {
|
||||
const newIdent = indent + " ";
|
||||
return wrapInDelimiters(array.map((index) => `${newIdent}${genRawValue(index, newIdent)}`), indent, "[]");
|
||||
}
|
||||
function genObjectFromRawEntries(array, indent = "") {
|
||||
const newIdent = indent + " ";
|
||||
return wrapInDelimiters(array.map(([key, value]) => `${newIdent}${genObjectKey(key)}: ${genRawValue(value, newIdent)}`), indent, "{}");
|
||||
}
|
||||
function genRawValue(value, indent = "") {
|
||||
if (typeof value === "undefined") {
|
||||
return "undefined";
|
||||
}
|
||||
if (value === null) {
|
||||
return "null";
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return genArrayFromRaw(value, indent);
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
return genObjectFromRaw(value, indent);
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
const genTypeObject = (object, indent = "") => {
|
||||
const newIndent = indent + " ";
|
||||
return wrapInDelimiters(Object.entries(object).map(([key, value]) => {
|
||||
const [, k = key, optional = ""] = key.match(/^(.*[^?])(\?)?$/) || [];
|
||||
if (typeof value === "string") {
|
||||
return `${newIndent}${genObjectKey(k)}${optional}: ${value}`;
|
||||
}
|
||||
return `${newIndent}${genObjectKey(k)}${optional}: ${genTypeObject(value, newIndent)}`;
|
||||
}), indent, "{}", false);
|
||||
};
|
||||
const genInterface = (name, contents, options = {}) => {
|
||||
const result = [
|
||||
options.export && "export",
|
||||
`interface ${name}`,
|
||||
options.extends && `extends ${Array.isArray(options.extends) ? options.extends.join(", ") : options.extends}`,
|
||||
contents ? genTypeObject(contents) : "{}"
|
||||
].filter(Boolean).join(" ");
|
||||
return result;
|
||||
};
|
||||
const genAugmentation = (specifier, interfaces) => {
|
||||
return `declare module ${genString(specifier)} ${wrapInDelimiters(
|
||||
Object.entries(interfaces || {}).map(
|
||||
([key, entry]) => " " + (Array.isArray(entry) ? genInterface(key, ...entry) : genInterface(key, entry))
|
||||
)
|
||||
)}`;
|
||||
};
|
||||
|
||||
export { escapeString, genArrayFromRaw, genAugmentation, genDynamicImport, genExport, genImport, genInlineTypeImport, genInterface, genObjectFromRaw, genObjectFromRawEntries, genSafeVariableName, genString, genTypeExport, genTypeImport, genTypeObject };
|
||||
41
node_modules/knitwork/package.json
generated
vendored
Normal file
41
node_modules/knitwork/package.json
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "knitwork",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"repository": "unjs/knitwork",
|
||||
"license": "MIT",
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "vitest dev",
|
||||
"lint": "eslint --ext .ts,.js,.mjs,.cjs .",
|
||||
"prepack": "unbuild",
|
||||
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
|
||||
"test": "vitest run --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitest/coverage-c8": "^0.25.2",
|
||||
"esbuild": "^0.15.13",
|
||||
"eslint": "latest",
|
||||
"eslint-config-unjs": "^0.0.2",
|
||||
"standard-version": "^9.5.0",
|
||||
"typescript": "^4.8.4",
|
||||
"unbuild": "^0.9.4",
|
||||
"vitest": "^0.25.2"
|
||||
},
|
||||
"packageManager": "pnpm@7.16.0"
|
||||
}
|
||||
Reference in New Issue
Block a user