Skip to content

Commit

Permalink
wip 2
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed Sep 22, 2021
1 parent aa2d60c commit 2b6f104
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 86 deletions.
Empty file added .editorconfig
Empty file.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 24 additions & 37 deletions src/collections.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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<void> {
return new Promise<void>((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<void> {
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<unknown> {
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);
}
28 changes: 15 additions & 13 deletions src/json-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
// 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<unknown> {
return new Promise<unknown>((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(
data: unknown,
filePath: string
): Promise<void> {
return new Promise<void>((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();
});
});
}
8 changes: 4 additions & 4 deletions src/json-sorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
26 changes: 13 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
24 changes: 12 additions & 12 deletions src/models.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/reader.ts
Original file line number Diff line number Diff line change
@@ -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.)
Expand Down
6 changes: 3 additions & 3 deletions src/wait.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export async function wait(milliseconds: number): Promise<string> {
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);
});
}

0 comments on commit 2b6f104

Please sign in to comment.