Skip to content

Commit

Permalink
move to esm modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaders committed Feb 9, 2024
1 parent 21d5d6b commit 1310501
Show file tree
Hide file tree
Showing 14 changed files with 1,058 additions and 1,045 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'off',
'prettier/prettier': ['error', { endOfLine: 'auto' }],
},
ignorePatterns: ['dist/'],
ignorePatterns: ['dist/', 'dist-original'],
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
dist-original/
*.tgz
sampleMarkdown.md
coverage/
File renamed without changes.
File renamed without changes.
2,024 changes: 1,016 additions & 1,008 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "ts-command-line-args",
"version": "2.5.1",
"type": "module",
"description": "A Typescript wrapper around command-line-args with additional support for markdown usage guide generation",
"bin": {
"write-markdown": "dist/write-markdown.js"
Expand Down Expand Up @@ -75,12 +76,12 @@
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"hosted-git-info": "^2.8.9",
"jest": "^27.5.1",
"jest": "^28.1.3",
"lodash": "^4.17.21",
"prettier": "^2.1.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.5",
"typescript": "4.3"
"ts-jest": "^28.0.8",
"typescript": "5.0"
},
"markdownConfig": {
"markdownPath": "README.MD",
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/add-content.helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IReplaceOptions } from '../contracts';
import { footerReplaceBelowMarker } from '../write-markdown.constants';
import { splitContent, findEscapeSequence, filterDoubleBlankLines } from './line-ending.helper';
import { footerReplaceBelowMarker } from '../write-markdown.constants.js';
import { splitContent, findEscapeSequence, filterDoubleBlankLines } from './line-ending.helper.js';

/**
* Adds or replaces content between 2 markers within a text string
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/command-line.helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PropertyOptions, ArgumentConfig, ArgumentOptions, CommandLineOption } from '../contracts';
import { isBoolean } from './options.helper';
import { isBoolean } from './options.helper.js';

export function createCommandLineConfig<T>(config: ArgumentOptions<T>): CommandLineOption[] {
return Object.keys(config).map((key) => {
Expand Down
16 changes: 8 additions & 8 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './command-line.helper';
export * from './add-content.helper';
export * from './markdown.helper';
export * from './visitor';
export * from './line-ending.helper';
export * from './options.helper';
export * from './string.helper';
export * from './insert-code.helper';
export * from './command-line.helper.js';
export * from './add-content.helper.js';
export * from './markdown.helper.js';
export * from './visitor.js';
export * from './line-ending.helper.js';
export * from './options.helper.js';
export * from './string.helper.js';
export * from './insert-code.helper.js';
2 changes: 1 addition & 1 deletion src/helpers/insert-code.helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IInsertCodeOptions } from '../contracts';
import { filterDoubleBlankLines, findEscapeSequence, splitContent } from './line-ending.helper';
import { filterDoubleBlankLines, findEscapeSequence, splitContent } from './line-ending.helper.js';
import { isAbsolute, resolve, dirname, join } from 'path';
import { promisify } from 'util';
import { readFile, writeFile } from 'fs';
Expand Down
22 changes: 12 additions & 10 deletions src/helpers/markdown.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
SectionHeader,
} from '../contracts';
import { join } from 'path';
import { normaliseConfig, createCommandLineConfig } from './command-line.helper';
import { generateTableFooter, getOptionSections, mapDefinitionDetails } from './options.helper';
import { convertChalkStringToMarkdown } from './string.helper';
import { normaliseConfig, createCommandLineConfig } from './command-line.helper.js';
import { generateTableFooter, getOptionSections, mapDefinitionDetails } from './options.helper.js';
import { convertChalkStringToMarkdown } from './string.helper.js';

export function createUsageGuide<T = any>(config: UsageGuideConfig<T>): string {
const options = config.parseOptions || {};
Expand Down Expand Up @@ -143,7 +143,7 @@ export function getType(option: CommandLineOption): string {
return `${type}${multiple} `;
}

export function generateUsageGuides(args: IWriteMarkDown): string[] | undefined {
export async function generateUsageGuides(args: IWriteMarkDown): Promise<string[] | undefined> {
if (args.jsFile == null) {
console.log(
`No jsFile defined for usage guide generation. See 'write-markdown -h' for details on generating usage guides.`,
Expand All @@ -155,17 +155,19 @@ export function generateUsageGuides(args: IWriteMarkDown): string[] | undefined
return [...imports, ...args.configImportName.map((importName) => ({ jsFile, importName }))];
}

return args.jsFile
const importPromises = args.jsFile
.reduce(mapJsImports, new Array<JsImport>())
.map(({ jsFile, importName }) => loadArgConfig(jsFile, importName))
.filter(isDefined)
.map(createUsageGuide);
.map(({ jsFile, importName }) => loadArgConfig(jsFile, importName));

const resolvedImports = await Promise.all(importPromises);

return resolvedImports.filter(isDefined).map(createUsageGuide);
}

export function loadArgConfig(jsFile: string, importName: string): UsageGuideConfig | undefined {
export async function loadArgConfig(jsFile: string, importName: string): Promise<UsageGuideConfig | undefined> {
const jsPath = join(process.cwd(), jsFile);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsExports = require(jsPath);
const jsExports = await import(jsPath);

const argConfig: UsageGuideConfig = jsExports[importName];

Expand Down
6 changes: 3 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
normaliseConfig,
removeBooleanValues,
visit,
} from './helpers';
import { addOptions, getOptionFooterSection, getOptionSections } from './helpers/options.helper';
import { removeAdditionalFormatting } from './helpers/string.helper';
} from './helpers/index.js';
import { addOptions, getOptionFooterSection, getOptionSections } from './helpers/options.helper.js';
import { removeAdditionalFormatting } from './helpers/string.helper.js';
import { readFileSync } from 'fs';
import { resolve } from 'path';

Expand Down
11 changes: 6 additions & 5 deletions src/write-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#!/usr/bin/env node

import { parse } from './parse';
import { parse } from './parse.js';
import { IWriteMarkDown } from './contracts';
import { resolve, relative } from 'path';
import { readFileSync, writeFileSync } from 'fs';
import { addCommandLineArgsFooter, addContent, generateUsageGuides, insertCode } from './helpers';
import { argumentConfig, parseOptions } from './write-markdown.constants';
import { addCommandLineArgsFooter, addContent, generateUsageGuides, insertCode } from './helpers/index.js';
import { argumentConfig, parseOptions } from './write-markdown.constants.js';
import format from 'string-format';
import chalk from 'chalk';
import { pathToFileURL } from "url"

async function writeMarkdown() {
const args = parse<IWriteMarkDown>(argumentConfig, parseOptions);

const markdownPath = resolve(args.markdownPath);
const markdownPath = pathToFileURL(resolve(args.markdownPath)).href;

console.log(`Loading existing file from '${chalk.blue(markdownPath)}'`);
const markdownFileContent = readFileSync(markdownPath).toString();

const usageGuides = generateUsageGuides(args);
const usageGuides = await generateUsageGuides(args);

let modifiedFileContent = markdownFileContent;

Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "es2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down Expand Up @@ -34,7 +34,7 @@
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand Down

0 comments on commit 1310501

Please sign in to comment.