Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Apr 29, 2023
1 parent 482fd4d commit 3e192d3
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 282 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = {
'plugin:prettier/recommended'
],
rules: {
'no-sync': 'error',
'prefer-promise-reject-errors': 'off',
'n/prefer-promises/fs': 'error',
'@typescript-eslint/no-floating-promises': [
'error',
{
Expand Down
142 changes: 74 additions & 68 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,91 @@
import path from 'path';
import fs from 'fs';
import os from 'os';
import Transformations from '../src/Transformations';
import pjson from 'pjson';
import { Transformations } from '../src/Transformations';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg: any = require('pjson');
const pkg = pjson as any;

const readmePath: string = path.join(__dirname, '/../README.md');

const configuration: any = pkg.contributes.configuration;
const configuration = pkg.contributes.configuration;

let config: string =
'| Key | Type | Description | Default |' +
os.EOL +
'| -------- | ----------- | ----------- | ----------- |' +
os.EOL;
void (async () => {
try {
let config: string =
'| Key | Type | Description | Default |' +
os.EOL +
'| -------- | ----------- | ----------- | ----------- |' +
os.EOL;

for (const configKey of Object.keys(configuration.properties)) {
const configValue = configuration.properties[configKey];
config += `| ${configKey} | `;
for (const configKey of Object.keys(configuration.properties)) {
const configValue = configuration.properties[configKey];
config += `| ${configKey} | `;

if (typeof configValue.type === 'string') {
config += `\`${configValue.type}\``;
} else if (Array.isArray(configValue.type)) {
config += `\`${configValue.type.join(' \\| ')}\``;
}
config += ` | ${configValue.description}`;
if (typeof configValue.type === 'string') {
config += `\`${configValue.type}\``;
} else if (Array.isArray(configValue.type)) {
config += `\`${configValue.type.join(' \\| ')}\``;
}
config += ` | ${configValue.description}`;

if (typeof configValue.default === 'string') {
config += ` | "${configValue.default}"`;
} else if (typeof configValue.default === 'number') {
config += ` | ${configValue.default}`;
} else if (
Array.isArray(configValue.default) ||
typeof configValue.default === 'boolean'
) {
config += ` | ${JSON.stringify(configValue.default)}`;
} else {
throw new Error('uncovered type');
}
if (typeof configValue.default === 'string') {
config += ` | "${configValue.default}"`;
} else if (typeof configValue.default === 'number') {
config += ` | ${configValue.default}`;
} else if (
Array.isArray(configValue.default) ||
typeof configValue.default === 'boolean'
) {
config += ` | ${JSON.stringify(configValue.default)}`;
} else {
throw new Error('uncovered type');
}

config += ' | ' + os.EOL;
}
config += ' | ' + os.EOL;
}

let readmeContent = fs.readFileSync(readmePath).toString();
readmeContent = readmeContent.replace(
/<!-- Configuration START -->([\s\S]*)<!-- Configuration END -->/,
() => {
return (
'<!-- Configuration START -->' +
os.EOL +
config +
os.EOL +
'<!-- Configuration END -->'
let readmeContent = String(await fs.promises.readFile(readmePath));
readmeContent = readmeContent.replace(
/<!-- Configuration START -->([\s\S]*)<!-- Configuration END -->/,
() => {
return (
'<!-- Configuration START -->' +
os.EOL +
config +
os.EOL +
'<!-- Configuration END -->'
);
}
);
}
);

readmeContent = readmeContent.replace(
/<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/,
() => {
return (
'<!-- Transformations START -->' +
os.EOL +
'| Key | Description |' +
os.EOL +
'| -------- | ----------- |' +
os.EOL +
new Transformations('php')
.getTransformations()
.map(item => {
let row = `| ${item.key} | `;
row += item.description;
row += ' |';
return row;
})
.join(os.EOL) +
os.EOL +
'<!-- Transformations END -->'
readmeContent = readmeContent.replace(
/<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/,
() => {
return (
'<!-- Transformations START -->' +
os.EOL +
'| Key | Description |' +
os.EOL +
'| -------- | ----------- |' +
os.EOL +
new Transformations('php')
.getTransformations()
.map(item => {
let row = `| ${item.key} | `;
row += item.description;
row += ' |';
return row;
})
.join(os.EOL) +
os.EOL +
'<!-- Transformations END -->'
);
}
);
}
);

fs.writeFileSync(readmePath, readmeContent);
await fs.promises.writeFile(readmePath, readmeContent);
} catch (err) {
console.error(err);
}
})();
4 changes: 0 additions & 4 deletions src/ITransformationItem.ts

This file was deleted.

Loading

0 comments on commit 3e192d3

Please sign in to comment.