juisy 3.0.0-beta.2 → 3.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -661
- package/README.md +25 -12
- package/bin/cli/cli.js +28 -28
- package/bin/cli/cmds/index.js +11 -11
- package/bin/cli/cmds/public/print-globals.js +27 -27
- package/bin/cli/index.js +9 -9
- package/bin/scripts/prepare.js +22 -22
- package/dist/DataExporter.d.ts +133 -34
- package/dist/ReadmeTemplater-DobSI9Um.js +373 -0
- package/dist/bootstrap.d.ts +11 -3
- package/dist/bootstrap.js +15 -11
- package/dist/cli/Command.d.ts +4 -4
- package/dist/cli/OutputUtils.d.ts +1 -1
- package/dist/cli/cache-dir.d.ts +15 -0
- package/dist/cli/index.js +341 -148
- package/dist/cli/plugins/register-changelog-command/augment.d.ts +52 -34
- package/dist/cli/plugins/register-changelog-command/resolve-changelog-options.d.ts +19 -0
- package/dist/cli/plugins/register-release-command/build-temp-config.d.ts +13 -0
- package/dist/cli/plugins/register-release-command/release-args.d.ts +7 -0
- package/dist/cli/plugins/register-release-command/resolve-release-config.d.ts +40 -0
- package/dist/cli/serialize-cjs-module.d.ts +15 -0
- package/dist/cli/types.d.ts +1 -1
- package/dist/extensions.d.ts +33 -9
- package/dist/extensions.js +87 -20
- package/dist/get-package-info-CGDJsCsN.js +15 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +109 -23
- package/dist/project-globals.d.ts +8 -2
- package/dist/templater/Templater.d.ts +1 -1
- package/dist/templater/index.d.ts +2 -2
- package/dist/templater/index.js +2 -335
- package/dist/templater/markdown-templater/ReadmeTemplater.d.ts +31 -2
- package/dist/templater/markdown-templater/index.d.ts +12 -1
- package/package.json +24 -29
- package/skills/juisy/SKILL.md +97 -0
- package/skills/juisy/references/recipes.md +85 -0
package/dist/index.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* juisy v3.0.0-beta.
|
|
2
|
+
* juisy v3.0.0-beta.4
|
|
3
3
|
* Copyright © 2022-Present Hervé Perchec
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import yaml from 'yaml';
|
|
7
7
|
import { json2csv } from 'json-2-csv';
|
|
8
8
|
import jstoxml from 'jstoxml';
|
|
9
|
+
import { BSON } from 'bson';
|
|
10
|
+
import { encode as encode$1 } from '@msgpack/msgpack';
|
|
11
|
+
import avro from 'avsc';
|
|
12
|
+
import { stringify } from 'smol-toml';
|
|
13
|
+
import { encode } from 'cbor-x';
|
|
9
14
|
import fs from 'fs-extra';
|
|
10
15
|
import path from 'node:path';
|
|
11
16
|
import { packageDirectorySync } from 'pkg-dir';
|
|
12
17
|
import log from 'loglevel';
|
|
13
|
-
import { Templater } from '
|
|
18
|
+
import { T as Templater } from './ReadmeTemplater-DobSI9Um.js';
|
|
14
19
|
import getFileUrls from '@juisy/utils/fs/get-file-urls';
|
|
15
20
|
import isFile from '@juisy/utils/fs/is-file';
|
|
16
21
|
import isDirectory from '@juisy/utils/fs/is-directory';
|
|
17
|
-
import {
|
|
22
|
+
import { g as getPackageInfo } from './get-package-info-CGDJsCsN.js';
|
|
18
23
|
import { pathToFileURL } from 'node:url';
|
|
19
24
|
import dotenv from '@dotenvx/dotenvx';
|
|
20
25
|
import deepMerge from '@juisy/utils/object/deep-merge';
|
|
@@ -22,59 +27,145 @@ import deepMerge from '@juisy/utils/object/deep-merge';
|
|
|
22
27
|
const xml = jstoxml.toXML;
|
|
23
28
|
const SUPPORTED_FORMATS = [
|
|
24
29
|
"json",
|
|
30
|
+
"jsonl",
|
|
25
31
|
"yaml",
|
|
32
|
+
"toml",
|
|
26
33
|
"csv",
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
"tsv",
|
|
35
|
+
"xml",
|
|
36
|
+
"bson",
|
|
37
|
+
"msgpack",
|
|
38
|
+
"cbor",
|
|
39
|
+
"protobuf",
|
|
40
|
+
"avro"
|
|
32
41
|
];
|
|
42
|
+
function formatBinaryOutput(bytes, output = "uint8array") {
|
|
43
|
+
if (output === "base64") {
|
|
44
|
+
return Buffer.from(bytes).toString("base64");
|
|
45
|
+
}
|
|
46
|
+
if (output === "hex") {
|
|
47
|
+
return Buffer.from(bytes).toString("hex");
|
|
48
|
+
}
|
|
49
|
+
return bytes;
|
|
50
|
+
}
|
|
33
51
|
const jsonExporter = function(data, options = {}) {
|
|
34
52
|
return JSON.stringify(data, options.replacer, options.space);
|
|
35
53
|
};
|
|
54
|
+
const jsonlExporter = function(data, options = {}) {
|
|
55
|
+
const rows = Array.isArray(data) ? data : [data];
|
|
56
|
+
return rows.map((row) => JSON.stringify(row, options.replacer, options.space)).join("\n");
|
|
57
|
+
};
|
|
36
58
|
const yamlExporter = function(data, options = {}) {
|
|
37
59
|
return yaml.stringify(data, options.replacer, options.exporter);
|
|
38
60
|
};
|
|
39
|
-
const
|
|
40
|
-
|
|
61
|
+
const tomlExporter = function(data, _options = {}) {
|
|
62
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) {
|
|
63
|
+
throw new Error("DataExporter: toml export requires a plain object (not null / array / primitive)");
|
|
64
|
+
}
|
|
65
|
+
return stringify(data);
|
|
66
|
+
};
|
|
67
|
+
const csvExporter = function(data, options = {}, forceTsv = false) {
|
|
68
|
+
const { tsv, delimiter, ...rest } = options;
|
|
69
|
+
const useTsv = forceTsv || tsv === true;
|
|
70
|
+
const resolvedDelimiter = useTsv ? {
|
|
71
|
+
...typeof delimiter === "object" && delimiter !== null ? delimiter : {},
|
|
72
|
+
field: " "
|
|
73
|
+
} : delimiter;
|
|
74
|
+
return json2csv(data, {
|
|
75
|
+
...rest,
|
|
76
|
+
...resolvedDelimiter !== void 0 ? { delimiter: resolvedDelimiter } : {}
|
|
77
|
+
});
|
|
41
78
|
};
|
|
42
79
|
const xmlExporter = function(data, options) {
|
|
43
80
|
return xml(data, options);
|
|
44
81
|
};
|
|
82
|
+
const bsonExporter = function(data, options = {}) {
|
|
83
|
+
const { output, ...serializeOptions } = options;
|
|
84
|
+
const bytes = BSON.serialize(data, serializeOptions);
|
|
85
|
+
return formatBinaryOutput(bytes, output);
|
|
86
|
+
};
|
|
87
|
+
const msgpackExporter = function(data, options = {}) {
|
|
88
|
+
const { output, ...encodeOptions } = options;
|
|
89
|
+
const bytes = encode$1(data, encodeOptions);
|
|
90
|
+
return formatBinaryOutput(bytes, output);
|
|
91
|
+
};
|
|
92
|
+
const cborExporter = function(data, options = {}) {
|
|
93
|
+
const { output } = options;
|
|
94
|
+
const encoded = encode(data);
|
|
95
|
+
const bytes = encoded instanceof Uint8Array ? encoded : new Uint8Array(encoded);
|
|
96
|
+
return formatBinaryOutput(bytes, output);
|
|
97
|
+
};
|
|
98
|
+
const protobufExporter = function(data, options) {
|
|
99
|
+
if (!options?.type) {
|
|
100
|
+
throw new Error("DataExporter: protobuf export requires options.type (protobufjs Type)");
|
|
101
|
+
}
|
|
102
|
+
const { type, output } = options;
|
|
103
|
+
const payload = data;
|
|
104
|
+
const err = type.verify(payload);
|
|
105
|
+
if (err) {
|
|
106
|
+
throw new Error(`DataExporter: protobuf verify failed: ${err}`);
|
|
107
|
+
}
|
|
108
|
+
const message = type.create(payload);
|
|
109
|
+
const bytes = type.encode(message).finish();
|
|
110
|
+
return formatBinaryOutput(bytes, output);
|
|
111
|
+
};
|
|
112
|
+
const avroExporter = function(data, options) {
|
|
113
|
+
if (!options) {
|
|
114
|
+
throw new Error("DataExporter: avro export requires options.type or options.schema");
|
|
115
|
+
}
|
|
116
|
+
const type = "type" in options && options.type ? options.type : avro.Type.forSchema(
|
|
117
|
+
typeof options.schema === "string" ? JSON.parse(options.schema) : options.schema
|
|
118
|
+
);
|
|
119
|
+
const buffer = type.toBuffer(data);
|
|
120
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
121
|
+
return formatBinaryOutput(bytes, options.output);
|
|
122
|
+
};
|
|
45
123
|
class DataExporter {
|
|
46
124
|
/**
|
|
47
125
|
* The target format
|
|
48
126
|
*/
|
|
49
127
|
format;
|
|
50
128
|
/**
|
|
51
|
-
* Creates new instance of
|
|
52
|
-
* @param
|
|
129
|
+
* Creates new instance of DataExporter
|
|
130
|
+
* @param format - DataExporter format
|
|
53
131
|
*/
|
|
54
132
|
constructor(format) {
|
|
55
133
|
if (SUPPORTED_FORMATS.includes(format)) {
|
|
56
134
|
this.format = format;
|
|
57
135
|
} else {
|
|
58
|
-
throw new Error(`
|
|
136
|
+
throw new Error(`DataExporter: Unsupported "${format}" format`);
|
|
59
137
|
}
|
|
60
138
|
}
|
|
61
|
-
// eslint-disable-line @typescript-eslint/no-explicit-any
|
|
62
139
|
exportSync(data, options) {
|
|
63
140
|
switch (this.format) {
|
|
64
141
|
case "json":
|
|
65
142
|
return jsonExporter(data, options);
|
|
143
|
+
case "jsonl":
|
|
144
|
+
return jsonlExporter(data, options);
|
|
66
145
|
case "yaml":
|
|
67
146
|
return yamlExporter(data, options);
|
|
147
|
+
case "toml":
|
|
148
|
+
return tomlExporter(data, options);
|
|
68
149
|
case "csv":
|
|
69
|
-
return csvExporter(data, options);
|
|
150
|
+
return csvExporter(data, options, false);
|
|
151
|
+
case "tsv":
|
|
152
|
+
return csvExporter(data, options, true);
|
|
70
153
|
case "xml":
|
|
71
154
|
return xmlExporter(data, options);
|
|
155
|
+
case "bson":
|
|
156
|
+
return bsonExporter(data, options);
|
|
157
|
+
case "msgpack":
|
|
158
|
+
return msgpackExporter(data, options);
|
|
159
|
+
case "cbor":
|
|
160
|
+
return cborExporter(data, options);
|
|
161
|
+
case "protobuf":
|
|
162
|
+
return protobufExporter(data, options);
|
|
163
|
+
case "avro":
|
|
164
|
+
return avroExporter(data, options);
|
|
72
165
|
}
|
|
73
166
|
}
|
|
74
167
|
export(...args) {
|
|
75
|
-
return
|
|
76
|
-
resolve(this.exportSync(...args));
|
|
77
|
-
});
|
|
168
|
+
return Promise.resolve(this.exportSync(...args));
|
|
78
169
|
}
|
|
79
170
|
}
|
|
80
171
|
|
|
@@ -156,11 +247,6 @@ async function eject(templateDir, identifier, options) {
|
|
|
156
247
|
}
|
|
157
248
|
}
|
|
158
249
|
|
|
159
|
-
function getPackageInfo() {
|
|
160
|
-
const pkgPath = path.resolve(packageDirectorySync(), "./package.json");
|
|
161
|
-
return JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
250
|
function defineGlobals(config, builder) {
|
|
165
251
|
const env = {
|
|
166
252
|
...process.env
|
|
@@ -49,9 +49,15 @@ config: {
|
|
|
49
49
|
};
|
|
50
50
|
}, builder: DefineGlobalsBuilder<CustomGlobals>): DefaultProjectGlobals & CustomGlobals;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* Dynamically import a project globals module (default `./project.globals.js`).
|
|
53
|
+
*
|
|
54
|
+
* Prefer a **static** `import PROJECT_GLOBALS from './project.globals.js'` when the
|
|
55
|
+
* path is fixed: that keeps {@link defineGlobals} type inference. This helper
|
|
56
|
+
* widens the return type and is best for a custom path, deferred load, or tooling
|
|
57
|
+
* such as `print:globals` / the Vite inject plugin.
|
|
58
|
+
*
|
|
53
59
|
* @param filePath - The filePath. Default is `'./project.globals.js'`
|
|
54
|
-
* @returns The resolved project globals object
|
|
60
|
+
* @returns The resolved project globals object (widened)
|
|
55
61
|
* @group None
|
|
56
62
|
*/
|
|
57
63
|
export declare function getProjectGlobals(filePath?: string): Promise<DefaultProjectGlobals & Record<string, string | Record<string, unknown> | undefined>>;
|
|
@@ -14,7 +14,7 @@ export declare class Templater {
|
|
|
14
14
|
/**
|
|
15
15
|
* Renders a template with the given context.
|
|
16
16
|
*/
|
|
17
|
-
render(template: string, data?: undefined, options?: any): Promise<string>;
|
|
17
|
+
render(template: string, data?: Record<any, any> | undefined, options?: any): Promise<string>;
|
|
18
18
|
/**
|
|
19
19
|
* Renders a template file with the given context.
|
|
20
20
|
* @todo not yet implemented
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export { Templater } from './Templater';
|
|
9
9
|
export { MarkdownTemplater } from './markdown-templater';
|
|
10
|
-
export { ReadmeTemplater } from './markdown-templater/ReadmeTemplater';
|
|
10
|
+
export { ReadmeTemplater, DEFAULT_AUTO_GEN_MESSAGE } from './markdown-templater/ReadmeTemplater';
|
|
11
11
|
export type * from './types';
|
|
12
12
|
export type { MarkdownTemplaterOptions } from './markdown-templater';
|
|
13
|
-
export type { UserConfiguration as ReadmeTemplaterUserConfig, Configuration as ReadmeTemplaterConfig } from './markdown-templater/ReadmeTemplater';
|
|
13
|
+
export type { UserConfiguration as ReadmeTemplaterUserConfig, Configuration as ReadmeTemplaterConfig, AutoGenMessageOption } from './markdown-templater/ReadmeTemplater';
|
|
14
14
|
export type * from '../shims/shims';
|
package/dist/templater/index.js
CHANGED
|
@@ -1,339 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* juisy v3.0.0-beta.
|
|
2
|
+
* juisy v3.0.0-beta.4
|
|
3
3
|
* Copyright © 2022-Present Hervé Perchec
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
import handlebars from 'handlebars';
|
|
8
|
-
import { remark } from 'remark';
|
|
9
|
-
import remarkToc from 'remark-toc';
|
|
10
|
-
import remarkFrontmatter from 'remark-frontmatter';
|
|
11
|
-
import path from 'node:path';
|
|
12
|
-
import { pathToFileURL } from 'node:url';
|
|
13
|
-
import fs from 'fs-extra';
|
|
14
|
-
import chalk from 'chalk';
|
|
15
|
-
import merge from 'lodash.merge';
|
|
16
|
-
import mdu from 'markdown-utils';
|
|
17
|
-
import { markdownTable } from 'markdown-table';
|
|
18
|
-
import asciitree from 'ascii-tree';
|
|
19
|
-
import GithubSlugger from 'github-slugger';
|
|
20
|
-
|
|
21
|
-
class Templater {
|
|
22
|
-
constructor(engine, options) {
|
|
23
|
-
this.engine = engine;
|
|
24
|
-
options = options || {};
|
|
25
|
-
switch (engine) {
|
|
26
|
-
case "handlebars":
|
|
27
|
-
this.engine = handlebars;
|
|
28
|
-
this.methodsMap.compile = async (template) => {
|
|
29
|
-
return handlebars.compile(template, options.engineOptions);
|
|
30
|
-
};
|
|
31
|
-
this.methodsMap.render = async (template, data = void 0, opts = void 0) => {
|
|
32
|
-
return this.methodsMap.compile(template).template(data);
|
|
33
|
-
};
|
|
34
|
-
break;
|
|
35
|
-
case "ejs":
|
|
36
|
-
this.engine = ejs;
|
|
37
|
-
this.methodsMap.compile = async (template) => {
|
|
38
|
-
return ejs.compile(template, options.engineOptions);
|
|
39
|
-
};
|
|
40
|
-
this.methodsMap.render = async (template, data = void 0, opts) => {
|
|
41
|
-
return ejs.render(
|
|
42
|
-
template,
|
|
43
|
-
data,
|
|
44
|
-
{
|
|
45
|
-
...options.engineOptions,
|
|
46
|
-
...opts || {}
|
|
47
|
-
}
|
|
48
|
-
);
|
|
49
|
-
};
|
|
50
|
-
break;
|
|
51
|
-
/**
|
|
52
|
-
* @todo ? pug not yet supported
|
|
53
|
-
*/
|
|
54
|
-
case "pug":
|
|
55
|
-
this.engine = (template, options2) => () => "";
|
|
56
|
-
break;
|
|
57
|
-
default:
|
|
58
|
-
throw new Error(`Unsupported templating engine: ${engine}`);
|
|
59
|
-
}
|
|
60
|
-
if (options.defaultData) {
|
|
61
|
-
this.defaultData = options.defaultData;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
engine;
|
|
65
|
-
defaultData;
|
|
66
|
-
methodsMap = {};
|
|
67
|
-
// eslint-disable-line @typescript-eslint/no-unsafe-function-type
|
|
68
|
-
/**
|
|
69
|
-
* @returns Get the engine
|
|
70
|
-
*/
|
|
71
|
-
getEngine() {
|
|
72
|
-
return this.engine;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Renders a template with the given context.
|
|
76
|
-
*/
|
|
77
|
-
async render(template, data = void 0, options = void 0) {
|
|
78
|
-
const mergedData = { ...this.defaultData, ...data || {} };
|
|
79
|
-
return await this.methodsMap.render(template, mergedData, options);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Renders a template file with the given context.
|
|
83
|
-
* @todo not yet implemented
|
|
84
|
-
*/
|
|
85
|
-
renderFile(filePath, context = {}) {
|
|
86
|
-
return "";
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
class MarkdownTemplater extends Templater {
|
|
91
|
-
constructor(engine = "ejs", options) {
|
|
92
|
-
super(engine, options);
|
|
93
|
-
this.processor = options?.processor ? options.processor === "remark" ? remark() : options.processor : remark();
|
|
94
|
-
this.processorOptions = this.buildProcessorOptions(options?.processorOptions || {});
|
|
95
|
-
this.initProcessor();
|
|
96
|
-
}
|
|
97
|
-
processor;
|
|
98
|
-
processorOptions;
|
|
99
|
-
buildProcessorOptions(options) {
|
|
100
|
-
options = options || {};
|
|
101
|
-
const defaultRemarkStringifyOptions = {};
|
|
102
|
-
const defaultRemarkTocOptions = {};
|
|
103
|
-
const defaultRemarkFrontmatterOptions = {
|
|
104
|
-
type: "yaml",
|
|
105
|
-
marker: "-"
|
|
106
|
-
};
|
|
107
|
-
return {
|
|
108
|
-
use: [
|
|
109
|
-
...options.use || []
|
|
110
|
-
],
|
|
111
|
-
stringify: options.stringify !== void 0 ? options.stringify : defaultRemarkStringifyOptions,
|
|
112
|
-
toc: options.toc === true ? defaultRemarkTocOptions : options.toc !== void 0 ? options.toc : defaultRemarkTocOptions,
|
|
113
|
-
frontmatter: options.frontmatter === true ? defaultRemarkFrontmatterOptions : options.frontmatter !== void 0 ? options.frontmatter : defaultRemarkFrontmatterOptions
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
initProcessor() {
|
|
117
|
-
const processorPlugins = [];
|
|
118
|
-
if (this.processorOptions.stringify) this.processor.data("settings", this.processorOptions.stringify);
|
|
119
|
-
if (this.processorOptions.toc) processorPlugins.push([remarkToc, this.processorOptions.toc]);
|
|
120
|
-
if (this.processorOptions.frontmatter) processorPlugins.push([remarkFrontmatter, this.processorOptions.frontmatter]);
|
|
121
|
-
for (const plugin of processorPlugins.concat(this.processorOptions.use)) {
|
|
122
|
-
this.processor.use(...plugin);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Renders a template with the given context.
|
|
127
|
-
*/
|
|
128
|
-
async render(template, data = void 0, options = void 0) {
|
|
129
|
-
const content = await super.render(template, data, options);
|
|
130
|
-
return (await this.processor.process(content)).toString();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
new GithubSlugger();
|
|
135
|
-
const DEFAULT_INIT_TARGET_RELATIVE_PATH = "./docs/readme";
|
|
136
|
-
class ReadmeTemplater {
|
|
137
|
-
/**
|
|
138
|
-
* @param config - The user configuration file path (as string) or object
|
|
139
|
-
* @example
|
|
140
|
-
* // Path as string
|
|
141
|
-
* const templater = new ReadmeTemplater('./.docs/readme/config.js')
|
|
142
|
-
* // Pass object. See configuration below
|
|
143
|
-
* const templater = new ReadmeTemplater({ /* ... *\/ })
|
|
144
|
-
*/
|
|
145
|
-
constructor(config) {
|
|
146
|
-
this.config = config;
|
|
147
|
-
this.templater = new MarkdownTemplater("ejs", {});
|
|
148
|
-
}
|
|
149
|
-
// To cache resolved config value
|
|
150
|
-
#resolvedConfig;
|
|
151
|
-
config;
|
|
152
|
-
templater;
|
|
153
|
-
get resolvedConfig() {
|
|
154
|
-
return this.#resolvedConfig;
|
|
155
|
-
}
|
|
156
|
-
set resolvedConfig(value) {
|
|
157
|
-
this.#resolvedConfig = value;
|
|
158
|
-
}
|
|
159
|
-
async resolveConfig() {
|
|
160
|
-
if (!this.resolvedConfig) {
|
|
161
|
-
this.resolvedConfig = await ReadmeTemplater.processConfig(this.config);
|
|
162
|
-
Object.assign(this.templater.processorOptions, this.resolvedConfig.processor);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Writes rendered README markdown to file
|
|
167
|
-
* @param config - The config object to process. Can be path to config file as string.
|
|
168
|
-
* @param options - Same as render
|
|
169
|
-
* @throws Throws error if render or file writing fails
|
|
170
|
-
* @example
|
|
171
|
-
* await templater.generate() // => output to README.md file
|
|
172
|
-
* await templater.generate({ data: { foo: 'bar' } }) // pass options
|
|
173
|
-
*/
|
|
174
|
-
async generate(options = {}) {
|
|
175
|
-
await this.resolveConfig();
|
|
176
|
-
const resolvedConfig = this.resolvedConfig;
|
|
177
|
-
const targetFilePath = path.join(resolvedConfig.destFolder, resolvedConfig.fileName);
|
|
178
|
-
console.log(`Auto-generating "${resolvedConfig.fileName}" file`);
|
|
179
|
-
console.log();
|
|
180
|
-
console.log("- Syntax: " + chalk.yellow("ejs"));
|
|
181
|
-
const renderResult = await this.render(options);
|
|
182
|
-
try {
|
|
183
|
-
fs.writeFileSync(targetFilePath, renderResult);
|
|
184
|
-
} catch (error) {
|
|
185
|
-
console.log("- Status:", chalk.red("✘ FAILED"));
|
|
186
|
-
throw error;
|
|
187
|
-
}
|
|
188
|
-
console.log("- Status:", chalk.green("✔ OK"));
|
|
189
|
-
console.log('- Written to: "' + chalk.cyan(targetFilePath) + '"');
|
|
190
|
-
console.log();
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Render README markdown
|
|
194
|
-
* @param options - Options object
|
|
195
|
-
* @returns Returns the rendered markdown as string. Promise is immediately resolved except if a module is async.
|
|
196
|
-
* @example
|
|
197
|
-
* await templater.render()
|
|
198
|
-
* await templater.render({ data: { foo: 'bar' } })
|
|
199
|
-
*/
|
|
200
|
-
async render(options = {}) {
|
|
201
|
-
await this.resolveConfig();
|
|
202
|
-
const resolvedConfig = this.resolvedConfig;
|
|
203
|
-
const data = resolvedConfig.ejsDataPath ? await import(pathToFileURL(resolvedConfig.ejsDataPath).toString()).then((mod) => mod.default || mod) : {};
|
|
204
|
-
options.data = options.data || {};
|
|
205
|
-
const ejsData = merge(merge({
|
|
206
|
-
...ReadmeTemplater.defaultEjsData,
|
|
207
|
-
$config: resolvedConfig
|
|
208
|
-
}, data), options.data);
|
|
209
|
-
let template = await fs.readFile(resolvedConfig.templatePath, { encoding: "utf8" });
|
|
210
|
-
if (resolvedConfig.appendAutoGenMessage) {
|
|
211
|
-
template += "\n----\n<!-- markdownlint-disable-next-line line-length -->\n*<%= $config.fileName %> - this file was auto generated with [juisy](https://www.npmjs.com/package/juisy) README templater. Don't edit it.*\n";
|
|
212
|
-
}
|
|
213
|
-
return await this.templater.render(template, ejsData, resolvedConfig.ejsOptions);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Static methods
|
|
217
|
-
*/
|
|
218
|
-
/**
|
|
219
|
-
* Takes a custom config as unique parameter and merges it with the default configuration object.
|
|
220
|
-
* @param config - The config object to process. Can be path to config file as string.
|
|
221
|
-
* @returns Returns the processed configuration. Promise is immediately resolved except if module is async.
|
|
222
|
-
* @example
|
|
223
|
-
* readmeGenerator.processConfig({ ... }) // pass object
|
|
224
|
-
* readmeGenerator.processConfig('./.docs/readme/config.js') // pass path as string
|
|
225
|
-
* // If module.exports is a promise
|
|
226
|
-
* await readmeGenerator.processConfig('./.docs/readme/config.js') // async module
|
|
227
|
-
*/
|
|
228
|
-
static async processConfig(config) {
|
|
229
|
-
let customConfig;
|
|
230
|
-
if (typeof config === "string") {
|
|
231
|
-
const configPath = pathToFileURL(path.isAbsolute(config) ? config : path.resolve(process.cwd(), config));
|
|
232
|
-
customConfig = (await import(configPath.toString())).default;
|
|
233
|
-
} else if (typeof config !== "object") {
|
|
234
|
-
throw new TypeError("config parameter must be object or string. Received " + typeof config);
|
|
235
|
-
} else {
|
|
236
|
-
customConfig = config;
|
|
237
|
-
}
|
|
238
|
-
const result = {};
|
|
239
|
-
result.fileName = customConfig.fileName || ReadmeTemplater.defaultConfig.fileName;
|
|
240
|
-
result.destFolder = customConfig.destFolder || ReadmeTemplater.defaultConfig.destFolder;
|
|
241
|
-
result.templatePath = customConfig.templatePath || ReadmeTemplater.defaultConfig.templatePath;
|
|
242
|
-
result.ejsDataPath = customConfig.ejsDataPath || customConfig.ejsDataPath === null ? customConfig.ejsDataPath : ReadmeTemplater.defaultConfig.ejsDataPath;
|
|
243
|
-
if (customConfig.ejsOptions) {
|
|
244
|
-
result.ejsOptions = {
|
|
245
|
-
async: ReadmeTemplater.defaultConfig.ejsOptions.async,
|
|
246
|
-
// async prop see below
|
|
247
|
-
root: ReadmeTemplater.defaultConfig.ejsOptions.root,
|
|
248
|
-
// root prop see below (always array)
|
|
249
|
-
views: ReadmeTemplater.defaultConfig.ejsOptions.views,
|
|
250
|
-
// views prop see below (always array)
|
|
251
|
-
...customConfig.ejsOptions
|
|
252
|
-
};
|
|
253
|
-
} else {
|
|
254
|
-
result.ejsOptions = ReadmeTemplater.defaultConfig.ejsOptions;
|
|
255
|
-
}
|
|
256
|
-
result.appendAutoGenMessage = customConfig.appendAutoGenMessage !== void 0 ? customConfig.appendAutoGenMessage : ReadmeTemplater.defaultConfig.appendAutoGenMessage;
|
|
257
|
-
result.processor = customConfig.processor !== void 0 ? customConfig.processor : ReadmeTemplater.defaultConfig.processor;
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Returns the default configuration object.
|
|
262
|
-
* This base configuration is used by `processConfig` method and so `generate` and `render` methods.
|
|
263
|
-
*/
|
|
264
|
-
static defaultConfig = {
|
|
265
|
-
fileName: "README.md",
|
|
266
|
-
destFolder: process.cwd(),
|
|
267
|
-
templatePath: path.resolve(process.cwd(), DEFAULT_INIT_TARGET_RELATIVE_PATH, "template.md"),
|
|
268
|
-
ejsDataPath: path.resolve(process.cwd(), DEFAULT_INIT_TARGET_RELATIVE_PATH, "data.js"),
|
|
269
|
-
ejsOptions: {
|
|
270
|
-
/**
|
|
271
|
-
* Always render in async mode
|
|
272
|
-
*/
|
|
273
|
-
async: true,
|
|
274
|
-
/**
|
|
275
|
-
* Set project root for includes with an absolute path (e.g, /file.ejs).
|
|
276
|
-
* Can be array to try to resolve include from multiple directories.
|
|
277
|
-
*/
|
|
278
|
-
root: [
|
|
279
|
-
// User template path
|
|
280
|
-
// path.resolve(process.cwd(), DEFAULT_INIT_TARGET_RELATIVE_PATH)
|
|
281
|
-
],
|
|
282
|
-
/**
|
|
283
|
-
* An array of paths to use when resolving includes with relative paths.
|
|
284
|
-
*/
|
|
285
|
-
views: [
|
|
286
|
-
// // User template path
|
|
287
|
-
// path.resolve(process.cwd(), DEFAULT_INIT_TARGET_RELATIVE_PATH)
|
|
288
|
-
]
|
|
289
|
-
},
|
|
290
|
-
appendAutoGenMessage: true,
|
|
291
|
-
processor: {}
|
|
292
|
-
};
|
|
293
|
-
/**
|
|
294
|
-
* The default EJS data object.
|
|
295
|
-
* This base EJS data object is used by `render` methods and merged with user provided EJS data.
|
|
296
|
-
*/
|
|
297
|
-
static defaultEjsData = {
|
|
298
|
-
/**
|
|
299
|
-
* Contains the default configuration by default.
|
|
300
|
-
* Will be merged with custom configuration when using `generate` or `render` method.
|
|
301
|
-
* @example
|
|
302
|
-
* // Access configuration
|
|
303
|
-
* $config.fileName // => README.md
|
|
304
|
-
*/
|
|
305
|
-
$config: {},
|
|
306
|
-
/**
|
|
307
|
-
* Contains the following methods:
|
|
308
|
-
*
|
|
309
|
-
* - `table`: generates a markdown table
|
|
310
|
-
* - `asciiTree`: generates an "ASCII" tree
|
|
311
|
-
* - ... all methods from `markdown-utils` package
|
|
312
|
-
*
|
|
313
|
-
* > Please check the following package documentations:
|
|
314
|
-
* > - [markdown-utils](https://github.com/jonschlinkert/markdown-utils)
|
|
315
|
-
* > - [markdown-table](https://www.npmjs.com/package/markdown-table)
|
|
316
|
-
* > - [ascii-tree](https://www.npmjs.com/package/ascii-tree)
|
|
317
|
-
* @example
|
|
318
|
-
* // Generates a blockquote
|
|
319
|
-
* $utils.blockquote('This is a blockquote')
|
|
320
|
-
* // Generates a table
|
|
321
|
-
* $utils.table([
|
|
322
|
-
* [ 'Column 1', 'Column 2' ],
|
|
323
|
-
* [ 'Value 1', 'Value 2' ]
|
|
324
|
-
* ])
|
|
325
|
-
* // Generates an ascii tree
|
|
326
|
-
* $utils.asciiTree('*.\n**a\n**b\n*...')
|
|
327
|
-
*/
|
|
328
|
-
$utils: {
|
|
329
|
-
// markdown-utils
|
|
330
|
-
...mdu,
|
|
331
|
-
// markdown-table
|
|
332
|
-
table: markdownTable,
|
|
333
|
-
// ascii-tree
|
|
334
|
-
asciiTree: asciitree.generate
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
export { MarkdownTemplater, ReadmeTemplater, Templater };
|
|
6
|
+
export { D as DEFAULT_AUTO_GEN_MESSAGE, M as MarkdownTemplater, R as ReadmeTemplater, T as Templater } from '../ReadmeTemplater-DobSI9Um.js';
|
|
@@ -2,6 +2,17 @@ import { default as ejs } from 'ejs';
|
|
|
2
2
|
import { markdownTable } from 'markdown-table';
|
|
3
3
|
import { default as asciitree } from 'ascii-tree';
|
|
4
4
|
import { MarkdownTemplaterOptions } from './index';
|
|
5
|
+
/**
|
|
6
|
+
* Default auto-gen footer snippet (EJS).
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_AUTO_GEN_MESSAGE = "\n----\n*<%= $config.fileName %> - this file was auto generated with [juisy](https://www.npmjs.com/package/juisy) README templater. Don't edit it.*\n";
|
|
9
|
+
/**
|
|
10
|
+
* Customize the auto-gen footer: a full snippet, or a function that receives the
|
|
11
|
+
* default message and returns the snippet to append (still EJS-rendered).
|
|
12
|
+
*
|
|
13
|
+
* @group Types
|
|
14
|
+
*/
|
|
15
|
+
export type AutoGenMessageOption = string | ((defaultMessage: string) => string);
|
|
5
16
|
/**
|
|
6
17
|
* @group Types
|
|
7
18
|
*/
|
|
@@ -32,9 +43,14 @@ export type Configuration = {
|
|
|
32
43
|
views: string[];
|
|
33
44
|
};
|
|
34
45
|
/**
|
|
35
|
-
* If true, append
|
|
46
|
+
* If true, append the auto-gen footer to rendered markdown. Default is **true**.
|
|
36
47
|
*/
|
|
37
48
|
appendAutoGenMessage: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Footer snippet appended when {@link Configuration.appendAutoGenMessage} is true.
|
|
51
|
+
* Resolved from {@link UserConfiguration.autoGenMessage} (string or transform fn).
|
|
52
|
+
*/
|
|
53
|
+
autoGenMessage: string;
|
|
38
54
|
/**
|
|
39
55
|
* The markdown processor (remark) options
|
|
40
56
|
*/
|
|
@@ -50,6 +66,14 @@ export type UserConfiguration = {
|
|
|
50
66
|
ejsDataPath?: Configuration['ejsDataPath'];
|
|
51
67
|
ejsOptions?: Configuration['ejsOptions'];
|
|
52
68
|
appendAutoGenMessage?: Configuration['appendAutoGenMessage'];
|
|
69
|
+
/**
|
|
70
|
+
* Override the auto-gen footer. Pass a string, or a function that receives the
|
|
71
|
+
* default message and returns the snippet to use.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* autoGenMessage: (defaultMessage) => defaultMessage.replace('juisy', 'my-cli')
|
|
75
|
+
*/
|
|
76
|
+
autoGenMessage?: AutoGenMessageOption;
|
|
53
77
|
processor?: Configuration['processor'];
|
|
54
78
|
};
|
|
55
79
|
export declare class ReadmeTemplater {
|
|
@@ -64,7 +88,8 @@ export declare class ReadmeTemplater {
|
|
|
64
88
|
*/
|
|
65
89
|
constructor(config: Parameters<typeof ReadmeTemplater.processConfig>[0]);
|
|
66
90
|
private config;
|
|
67
|
-
|
|
91
|
+
/** Created in {@link resolveConfig} with `processor` from user config. */
|
|
92
|
+
private templater?;
|
|
68
93
|
get resolvedConfig(): Configuration | undefined;
|
|
69
94
|
set resolvedConfig(value: Configuration | undefined);
|
|
70
95
|
private resolveConfig;
|
|
@@ -106,6 +131,10 @@ export declare class ReadmeTemplater {
|
|
|
106
131
|
* await readmeGenerator.processConfig('./.docs/readme/config.js') // async module
|
|
107
132
|
*/
|
|
108
133
|
static processConfig(config: UserConfiguration | string): Promise<Configuration>;
|
|
134
|
+
/**
|
|
135
|
+
* Resolves {@link UserConfiguration.autoGenMessage} to a concrete EJS snippet.
|
|
136
|
+
*/
|
|
137
|
+
static resolveAutoGenMessage(option?: AutoGenMessageOption): string;
|
|
109
138
|
/**
|
|
110
139
|
* Returns the default configuration object.
|
|
111
140
|
* This base configuration is used by `processConfig` method and so `generate` and `render` methods.
|
|
@@ -10,7 +10,10 @@ import { TemplaterOptions } from '../types';
|
|
|
10
10
|
export type MarkdownTemplaterOptions = TemplaterOptions<any> & {
|
|
11
11
|
processor?: 'remark' | typeof remark;
|
|
12
12
|
/**
|
|
13
|
-
* Options to pass to remark
|
|
13
|
+
* Options to pass to remark.
|
|
14
|
+
*
|
|
15
|
+
* `stringify` is forwarded to remark via `processor.data('settings', …)`
|
|
16
|
+
* (mdast-util-to-markdown options such as `join`). See cookbook templaters.
|
|
14
17
|
*/
|
|
15
18
|
processorOptions?: {
|
|
16
19
|
use?: MarkdownTemplater['processorOptions']['use'];
|
|
@@ -30,6 +33,14 @@ export declare class MarkdownTemplater extends Templater {
|
|
|
30
33
|
};
|
|
31
34
|
private buildProcessorOptions;
|
|
32
35
|
private initProcessor;
|
|
36
|
+
/**
|
|
37
|
+
* Rebuild the remark pipeline from processor options.
|
|
38
|
+
*
|
|
39
|
+
* Always creates a **fresh** `remark()` instance so plugins are not stacked
|
|
40
|
+
* twice. Prefer passing `processorOptions` to the constructor when possible;
|
|
41
|
+
* use this when options are only known after construction (e.g. async config).
|
|
42
|
+
*/
|
|
43
|
+
applyProcessorOptions(options?: MarkdownTemplaterOptions['processorOptions']): void;
|
|
33
44
|
/**
|
|
34
45
|
* Renders a template with the given context.
|
|
35
46
|
*/
|