md-hopper 1.0.0-alpha.1 → 1.0.0-alpha.3

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.
@@ -1,10 +1,12 @@
1
1
  export interface BundleOptions {
2
+ debug?: boolean;
2
3
  skipHidden?: boolean;
3
4
  include?: string;
4
5
  exclude?: string;
5
6
  filenames?: string;
6
7
  output?: string;
7
8
  input?: string;
9
+ depth?: string;
8
10
  }
9
11
  export interface HelpOptions {
10
12
  [key: string]: any;
@@ -3,6 +3,8 @@ export interface Settings {
3
3
  include: string[];
4
4
  exclude: string[];
5
5
  filenames: string[];
6
+ input: string;
6
7
  output: string;
8
+ depth: number;
7
9
  }
8
10
  export type RcSettings = Partial<Settings>;
@@ -1 +1,3 @@
1
+ /// <reference types="node" resolution-mode="require"/>
1
2
  export { getAllFiles } from 'my-gadgetry/fs';
3
+ export declare const readJsonFileSync: (path: string, encode?: BufferEncoding) => {};
@@ -1 +1,10 @@
1
+ import { readFileSync } from 'fs';
2
+ import { isJson } from 'my-gadgetry/type-check';
1
3
  export { getAllFiles } from 'my-gadgetry/fs';
4
+ export const readJsonFileSync = (path, encode = 'utf-8') => {
5
+ const json = isJson(readFileSync(path, encode));
6
+ if (!json) {
7
+ return {};
8
+ }
9
+ return json;
10
+ };
@@ -2,3 +2,4 @@ export declare const success: (...args: any[]) => void;
2
2
  export declare const warn: (...args: any[]) => void;
3
3
  export declare const info: (...args: any[]) => void;
4
4
  export declare const error: (...args: any[]) => void;
5
+ export declare const hop: (...args: any[]) => void;
@@ -6,8 +6,11 @@ export const warn = (...args) => {
6
6
  console.log(chalk.hex('#FFA500')(...args));
7
7
  };
8
8
  export const info = (...args) => {
9
- console.log(chalk.blue(...args));
9
+ console.log(chalk.hex('#77CFF5')(...args));
10
10
  };
11
11
  export const error = (...args) => {
12
12
  console.log(chalk.red(...args));
13
13
  };
14
+ export const hop = (...args) => {
15
+ console.log(chalk.hex('#F577F5')(...args));
16
+ };
@@ -1,5 +1,5 @@
1
- export declare const getProjectRootPath: () => string;
2
- export declare const getFullPath: (...path: string[]) => string;
1
+ export declare const getExecDir: () => string;
2
+ export declare const getParseDirPath: (path: string) => string;
3
3
  export declare const getDir: (path: string) => string;
4
4
  export declare const DEPTH_TYPES: {
5
5
  ancestor: string;
@@ -1,19 +1,6 @@
1
- import { existsSync } from 'fs';
2
- import { join, parse, relative } from 'path';
3
- import { SETTING_FILE_NAME } from './configs.js';
4
- export const getProjectRootPath = () => {
5
- let path = process.cwd();
6
- if (!existsSync(join(path, SETTING_FILE_NAME))) {
7
- for (const _ in process.cwd().split(/\//g)) {
8
- path = join(path, '..');
9
- if (existsSync(join(path, SETTING_FILE_NAME))) {
10
- break;
11
- }
12
- }
13
- }
14
- return path;
15
- };
16
- export const getFullPath = (...path) => join(getProjectRootPath(), ...path);
1
+ import { parse, relative } from 'path';
2
+ export const getExecDir = () => process.cwd();
3
+ export const getParseDirPath = (path) => parse(path).dir;
17
4
  export const getDir = (path) => {
18
5
  return parse(path).dir;
19
6
  };
@@ -1,8 +1,11 @@
1
+ import { join } from 'path';
1
2
  import { ENVS } from '../types/process.js';
2
- import pkg from '../../../package.json' assert { type: 'json' };
3
+ import { readJsonFileSync } from './fs.js';
3
4
  export * from '../types/process.js';
5
+ const __dirname = import.meta.dirname;
4
6
  export const args = process.argv.slice(2);
5
7
  export const init = () => {
8
+ const pkg = readJsonFileSync(join(__dirname, '../../..', 'package.json'));
6
9
  set(ENVS.PACKAGE_VERSION, pkg.version);
7
10
  set(ENVS.IS_DEBUG, bool(process.env.DEBUG) || args.includes('--debug'));
8
11
  };
@@ -1,9 +1,10 @@
1
1
  import { readFileSync } from 'fs';
2
2
  import { getParam } from './md.js';
3
3
  import { SETTING_FILE_NAME } from './configs.js';
4
- import { getProjectRootPath } from './path.js';
5
- const Booleans = ['skip-hidden'];
6
- const Strings = ['exclude', 'include', 'filenames'];
4
+ import { getExecDir } from './path.js';
5
+ const BOOLEANS = ['skip-hidden'];
6
+ const STRINGS = ['exclude', 'include', 'filenames'];
7
+ const NUMBERS = ['depth'];
7
8
  export const DEFAULT_MD_FILE_NAME = 'README.md';
8
9
  export const DEFAULT_SETTINGS = Object.freeze({
9
10
  'skip-hidden': true,
@@ -11,6 +12,8 @@ export const DEFAULT_SETTINGS = Object.freeze({
11
12
  include: [],
12
13
  filenames: [DEFAULT_MD_FILE_NAME],
13
14
  output: DEFAULT_MD_FILE_NAME,
15
+ input: '.',
16
+ depth: 0,
14
17
  });
15
18
  export const parse = (txt) => {
16
19
  const lines = txt.split('\n');
@@ -23,12 +26,15 @@ export const parse = (txt) => {
23
26
  const key = keyValue[0].trim();
24
27
  const value = keyValue[1].trim();
25
28
  let val;
26
- if (Booleans.includes(key)) {
29
+ if (BOOLEANS.includes(key)) {
27
30
  val = JSON.parse(`${value}`);
28
31
  }
29
- else if (Strings.includes(key)) {
32
+ else if (STRINGS.includes(key)) {
30
33
  val = value.split(',');
31
34
  }
35
+ else if (NUMBERS.includes(key)) {
36
+ val = Number(value);
37
+ }
32
38
  else {
33
39
  continue;
34
40
  }
@@ -40,6 +46,6 @@ export const parse = (txt) => {
40
46
  };
41
47
  };
42
48
  export const getSettings = (inputFilename) => {
43
- const readme = readFileSync(`${getProjectRootPath()}/${inputFilename || SETTING_FILE_NAME}`, 'utf-8');
49
+ const readme = readFileSync(`${getExecDir()}/${inputFilename || SETTING_FILE_NAME}`, 'utf-8');
44
50
  return parse(getParam(readme, 'CONFIG'));
45
51
  };
@@ -1,8 +1,9 @@
1
1
  import { readFileSync, writeFileSync } from 'fs';
2
2
  import { join } from 'path';
3
3
  import { getAllFiles } from '../../../_internal/utils/fs.js';
4
+ import { info, warn, success } from '../../../_internal/utils/log.js';
4
5
  import { getParam, getTitle, replace } from '../../../_internal/utils/md.js';
5
- import { getProjectRootPath } from '../../../_internal/utils/path.js';
6
+ import { getExecDir, getParseDirPath } from '../../../_internal/utils/path.js';
6
7
  import { getDir, getDepthType, DEPTH_TYPES } from '../../../_internal/utils/path.js';
7
8
  const replacePath = (str) => str.replace(/\/{2,}/, '/');
8
9
  const splitComma = (str, alternative) => (str ? str.split(',') : null) || alternative;
@@ -12,7 +13,9 @@ const mergeConfig = ({ options, settings }) => {
12
13
  exclude: splitComma(options.exclude, settings.exclude),
13
14
  include: splitComma(options.include, settings.include),
14
15
  filenames: splitComma(options.filenames, settings.filenames),
16
+ input: getParseDirPath(options.input || '.'),
15
17
  output: options.output || settings.output,
18
+ depth: Number(options.depth) || settings.depth,
16
19
  };
17
20
  };
18
21
  const classify = (dataList) => {
@@ -63,17 +66,45 @@ const getPathDetails = (paths, config) => {
63
66
  return classify(list);
64
67
  };
65
68
  export const action = async (args) => {
66
- const rootPath = getProjectRootPath();
69
+ const isDebug = args.options.debug;
70
+ if (isDebug) {
71
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
72
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- DEBUG MODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
73
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
74
+ }
75
+ else {
76
+ warn('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
77
+ warn('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- PRODUCTION MODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
78
+ warn('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
79
+ }
80
+ const execDir = getExecDir();
81
+ info('exec dir path', execDir);
67
82
  const config = mergeConfig(args);
83
+ info('settings', JSON.stringify(config, null, 2));
84
+ const targetDir = join(execDir, config.input);
85
+ info('target dir', targetDir);
68
86
  const skipHidden = config['skip-hidden'] ? ['**/.*/**/*', '**/.*'] : [];
69
87
  const exclude = [
70
88
  '**/node_modules/.*/*',
71
89
  '**/node_modules/**/*',
72
90
  ...skipHidden,
73
- ...config.exclude.map(path => replacePath(`${rootPath}/${path}`)),
91
+ ...config.exclude.map(path => replacePath(join(targetDir, path))),
74
92
  ];
75
- const include = config.include.map(path => replacePath(`${rootPath}/${path}`));
76
- const paths = getAllFiles(rootPath, [], { include, exclude }).filter(path => config.filenames.some(filename => path.endsWith(`/${filename}`)));
93
+ info('exclude', JSON.stringify(exclude, null, 2));
94
+ const include = config.include.map(path => replacePath(join(targetDir, path)));
95
+ info('include', JSON.stringify(include, null, 2));
96
+ const paths = getAllFiles(targetDir, {
97
+ include,
98
+ exclude,
99
+ ...(config.depth >= 1 ? { depth: config.depth } : {}),
100
+ }).filter(path => config.filenames.some(filename => path.endsWith(`/${filename}`)));
101
+ info('target paths', JSON.stringify(paths, null, 2));
102
+ if (isDebug) {
103
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
104
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- END OF DEBUG MODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
105
+ info('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
106
+ return;
107
+ }
77
108
  const pathDetails = getPathDetails(paths, config);
78
109
  for (const pathDetail of pathDetails) {
79
110
  if (pathDetail.lock) {
@@ -82,6 +113,10 @@ export const action = async (args) => {
82
113
  const content = replace(pathDetail, pathDetails);
83
114
  const output = pathDetail.output || config.output;
84
115
  const writePath = output ? join(pathDetail.dir, output) : pathDetail.path;
116
+ success('write file: ', writePath);
85
117
  writeFileSync(writePath, content, 'utf-8');
86
118
  }
119
+ success('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
120
+ success('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- END OF PRODUCTION MODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
121
+ success('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
87
122
  };
@@ -9,6 +9,7 @@ const setAliases = (commandClass) => {
9
9
  .option('-f, --filenames <strings>', 'filenames, default: README.md only')
10
10
  .option('-o, --output <filename>', 'output filename. default: README.md (replace)')
11
11
  .option('-i, --input <filename>', 'target root filename, default: README.md')
12
+ .option('-D, --depth <filename>', 'depth level more than 0')
12
13
  .action(options => {
13
14
  return action(options);
14
15
  });
@@ -1,23 +1,23 @@
1
1
  import { platform } from 'node:process';
2
- import { info } from '../../../_internal/utils/log.js';
2
+ import { info, hop } from '../../../_internal/utils/log.js';
3
3
  import { get, ENVS } from '../../../_internal/utils/process.js';
4
4
  const ASCII_ART = `
5
5
 
6
- _ _ __ _ _ _ ___ ___ _____
7
- | | | | | \\ | | | | / / / |/ | | _ \\
8
- | | | | | \\| | | |/ / / /| /| | | | | |
9
- | | | | | |\\ | | |\\ \\ / / |__/ | | | | | |
10
- | |___ | | | | \\ | | | \\ \\ / / | | | |_| |
11
- |_____| |_| |_| \\_| |_| \\_\\ /_/ |_| |_____/
6
+ ___ ___ _____ _ _ _____ _____ _____ _____ _____
7
+ / |/ | | _ \\ | | | | / _ \\ | _ \\ | _ \\ | ____| | _ \\
8
+ / /| /| | | | | | | |_| | | | | | | |_| | | |_| | | |__ | |_| |
9
+ / / |__/ | | | | | | | _ | | | | | | ___/ | ___/ | __| | _ /
10
+ / / | | | |_| | | | | | | |_| | | | | | | |___ | | \\ \\
11
+ /_/ |_| |_____/ |_| |_| \\_____/ |_| |_| |_____| |_| \\_\\
12
12
 
13
13
  `;
14
14
  const VERSIONS = `
15
- Link MD: ${get(ENVS.PACKAGE_VERSION)}
15
+ MD Hopper: ${get(ENVS.PACKAGE_VERSION)}
16
16
  Node: ${process.versions.node}
17
17
  OS: ${platform} ${process.arch}
18
18
  `;
19
19
  const show = () => {
20
- info(ASCII_ART);
20
+ hop(ASCII_ART);
21
21
  info(VERSIONS);
22
22
  };
23
23
  export const action = async (_) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "md-hopper",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "bin": {
@@ -63,16 +63,16 @@
63
63
  "@actions/exec": "^1.1.1",
64
64
  "@actions/github": "^6.0.0",
65
65
  "chalk": "^5.3.0",
66
- "commander": "^12.0.0",
66
+ "commander": "^12.1.0",
67
67
  "marked": "^12.0.2",
68
68
  "minimatch": "^9.0.4",
69
- "my-gadgetry": "^1.1.2",
70
- "zx": "^8.1.0"
69
+ "my-gadgetry": "^1.2.5",
70
+ "zx": "^8.1.2"
71
71
  },
72
72
  "devDependencies": {
73
- "@babel/core": "^7.24.5",
74
- "@babel/preset-env": "^7.24.5",
75
- "@babel/preset-typescript": "^7.24.1",
73
+ "@babel/core": "^7.24.6",
74
+ "@babel/preset-env": "^7.24.6",
75
+ "@babel/preset-typescript": "^7.24.6",
76
76
  "@eslint/js": "^8.57.0",
77
77
  "@types/jest": "^29.5.12",
78
78
  "@typescript-eslint/eslint-plugin": "^7.9.0",
@@ -87,15 +87,15 @@
87
87
  "fs-extra": "^11.2.0",
88
88
  "husky": "^9.0.11",
89
89
  "jest": "^29.7.0",
90
- "lint-staged": "^15.2.2",
90
+ "lint-staged": "^15.2.5",
91
91
  "npm-run-all2": "^6.2.0",
92
92
  "prettier": "^3.2.5",
93
- "ts-jest": "^29.1.2",
93
+ "ts-jest": "^29.1.4",
94
94
  "ts-node": "^10.9.2",
95
95
  "tsc-alias": "^1.8.10",
96
- "tsx": "^4.10.4",
96
+ "tsx": "^4.11.0",
97
97
  "typescript": "^5.4.5",
98
- "typescript-eslint": "^7.9.0"
98
+ "typescript-eslint": "^7.11.0"
99
99
  },
100
100
  "files": [
101
101
  "cli",