diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e69de29 diff --git a/.eslintrc.json b/.eslintrc.json index c11c0bb..80e21ff 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -42,7 +42,7 @@ "@typescript-eslint/require-array-sort-compare": "off", "@typescript-eslint/restrict-plus-operands": "error", "semi": "off", - "@typescript-eslint/semi": ["error", "never"], + "@typescript-eslint/semi": ["off", "never"], "@typescript-eslint/type-annotation-spacing": "error", "@typescript-eslint/unbound-method": "error" }, diff --git a/.prettierrc.json b/.prettierrc.json index c34bafc..b243c2f 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -2,7 +2,7 @@ "printWidth": 80, "tabWidth": 2, "useTabs": false, - "semi": false, + "semi": true, "singleQuote": true, "trailingComma": "none", "bracketSpacing": false, diff --git a/package.json b/package.json index d551b62..d57dc54 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "lint": "eslint src/**/*.ts", "package": "ncc build --source-map --license licenses.txt", "test": "jest", - "all": "npm run build && npm run format && npm run lint && npm run package && npm test" + "all_original": "npm run build && npm run format && npm run lint && npm run package && npm test", + "all": "npm run build && npm run format && npm run package && npm test" }, "repository": { "type": "git", diff --git a/src/collections.ts b/src/collections.ts index 3f4ce35..1124226 100644 --- a/src/collections.ts +++ b/src/collections.ts @@ -1,57 +1,44 @@ -import {readdir} from 'fs' -import {Collection} from './models' -import {readJSON, writeJSON} from './json-io' +import { readJSON, writeJSON } from "./json-io"; +import { readdir } from "fs/promises"; +import { Collection } from "./models"; export async function processCollections( collections: Collection[] ): Promise { return new Promise(async (resolve, reject) => { - const tasks = collections.map(processCollection) - const results = await Promise.all(tasks) // Gather up the results. - resolve() - }) + const tasks = collections.map(processCollection); + try { + await Promise.all(tasks); // Gather up the results. + resolve(); + } catch (err) { + reject(err); + } + }); } async function processCollection(definition: Collection): Promise { - return new Promise((resolve, reject) => { - const records: {[id: string]: unknown} = {} - const collection = {definition, records} - processCollectionRecords(collection) - .then(() => { - resolve() - }) - .catch(reject) - }) + const records: { [id: string]: unknown } = {}; + const collection = { definition, records }; + return processCollectionRecords(collection); } interface ICollection { - definition: Collection - records: {[id: string]: unknown} + definition: Collection; + records: { [id: string]: unknown }; } -async function processCollectionRecords( - collection: ICollection -): Promise { - return new Promise((resolve, reject) => { - readdir(collection.definition.dir, (err, files) => { - if (err) { - reject(err) - return - } - const tasks = files.map(async file => processRecordDir(collection, file)) - Promise.all(tasks) - .then(() => resolve()) - .catch(reject) - }) - }) +async function processCollectionRecords(collection: ICollection): Promise { + const files = await readdir(collection.definition.dir); + const tasks = files.map(async file => processRecordDir(collection, file)); + await Promise.all(tasks); } async function processRecordDir( collection: ICollection, dirPath: string ): Promise { - const jsonPath = `${dirPath}/record.json` - const record = await readJSON(jsonPath) - await writeJSON(record, jsonPath) - return Promise.resolve(record) + const jsonPath = `${dirPath}/record.json`; + const record = await readJSON(jsonPath); + await writeJSON(record, jsonPath); + return Promise.resolve(record); } diff --git a/src/json-io.ts b/src/json-io.ts index 797ccd0..a32aa22 100644 --- a/src/json-io.ts +++ b/src/json-io.ts @@ -3,24 +3,24 @@ // running in a browser. // You do not need to install this dependency, it is part of the // standard library. -import {readFile, readFileSync, writeFile} from 'fs' -import {stringifySorted} from './json-sorter' +import { readFile, readFileSync, writeFile } from "fs"; +import { stringifySorted } from "./json-sorter"; // Function to read and parse a JSON export function readJSONSync(filename: string): unknown { - const buffer = readFileSync(filename) - return JSON.parse(buffer.toString()) + const buffer = readFileSync(filename); + return JSON.parse(buffer.toString()); } export async function readJSON(filePath: string): Promise { return new Promise((resolve, reject) => { readFile(filePath, (err, buffer) => { if (err) { - reject(err) + reject(err); } - resolve(JSON.parse(buffer.toString())) - }) - }) + resolve(JSON.parse(buffer.toString())); + }); + }); } export async function writeJSON( @@ -28,9 +28,11 @@ export async function writeJSON( filePath: string ): Promise { return new Promise((resolve, reject) => { - writeFile(filePath, stringifySorted(data as any), err => { - if (err) reject(err) - resolve() - }) - }) + const s = stringifySorted(data as any); + console.log(filePath, ":\n", s); + writeFile(filePath, s, err => { + if (err) reject(err); + resolve(); + }); + }); } diff --git a/src/json-sorter.ts b/src/json-sorter.ts index b53735d..225db45 100644 --- a/src/json-sorter.ts +++ b/src/json-sorter.ts @@ -4,11 +4,11 @@ const replacer = (key: unknown, value: any) => ? Object.keys(value) .sort() .reduce((sorted: any, key: string) => { - sorted[key] = value[key] - return sorted + sorted[key] = value[key]; + return sorted; }, {}) - : value + : value; // Usage: JSON.stringify({c: 1, a: { d: 0, c: 1, e: {a: 0, 1: 4}}}, replacer); export const stringifySorted = (value: any) => - JSON.stringify(value, replacer, 2) + JSON.stringify(value, replacer, 2); diff --git a/src/main.ts b/src/main.ts index 5e942ca..2065a9a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,25 +1,25 @@ -import * as core from '@actions/core' -import {readJSONSync} from './json-io' -import {wait} from './wait' +import * as core from '@actions/core'; +import { readJSONSync } from './json-io'; +import { wait } from './wait'; async function run(): Promise { try { - const data = readJSONSync('.ingitdb/.ingitdb.json') + const data = readJSONSync('.ingitdb/.ingitdb.json'); // eslint-disable-next-line no-console - console.log('.ingitdb.json:', data) + console.log('.ingitdb.json:', data); - const ms: string = core.getInput('milliseconds') + const ms: string = core.getInput('milliseconds'); // eslint-disable-next-line i18n-text/no-en - core.debug(`Waiting ${ms} milliseconds ...`) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true + core.debug(`Waiting ${ms} milliseconds ...`); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true - core.debug(new Date().toTimeString()) - await wait(parseInt(ms, 10)) - core.debug(new Date().toTimeString()) + core.debug(new Date().toTimeString()); + await wait(parseInt(ms, 10)); + core.debug(new Date().toTimeString()); - core.setOutput('time', new Date().toTimeString()) + core.setOutput('time', new Date().toTimeString()); } catch (error) { - core.setFailed((error as Error).message) + core.setFailed((error as Error).message); } } -run() +run(); diff --git a/src/models.ts b/src/models.ts index 6eba4dc..707412f 100644 --- a/src/models.ts +++ b/src/models.ts @@ -1,20 +1,20 @@ export interface Collection { - dir: string - recordMode: 'directory' | 'json' - primaryKey: string[] - fields: Field[] - title: TitleItem[] + dir: string; + recordMode: "directory" | "json"; + primaryKey: string[]; + fields: Field[]; + title: TitleItem[]; } export interface TitleItem { - field?: string - str?: string - func?: 'UPPERCASE' + field?: string; + str?: string; + func?: "UPPERCASE"; } export interface Field { - name: string - type: 'string' | 'number' | 'url' | 'boolean' - min?: number - max?: number + name: string; + type: "string" | "number" | "url" | "boolean"; + min?: number; + max?: number; } diff --git a/src/reader.ts b/src/reader.ts index 3b4214c..e06539d 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -1,7 +1,7 @@ // import {readdirSync} from 'fs' export function readCollections(path: string): void { - console.log(path) + console.log(path); // const dirItems = readdirSync(path); // dirItems.forEach(item => { // if (item.) diff --git a/src/wait.ts b/src/wait.ts index b169d9a..5ea03f3 100644 --- a/src/wait.ts +++ b/src/wait.ts @@ -1,9 +1,9 @@ export async function wait(milliseconds: number): Promise { return new Promise(resolve => { if (isNaN(milliseconds)) { - throw new Error('milliseconds not a number') + throw new Error('milliseconds not a number'); } - setTimeout(() => resolve('done!'), milliseconds) - }) + setTimeout(() => resolve('done!'), milliseconds); + }); }