Skip to content

Commit

Permalink
chore: port assets generation to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Mar 13, 2020
1 parent 72cbab6 commit bc75bc9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"build": "tsc -p ./tsconfig.build.json",
"cli": "node -r ts-node/register -r tsconfig-paths/register src/cli/index.ts",
"cli:debug": "node -r ts-node/register -r tsconfig-paths/register --inspect-brk src/cli/index.ts",
"generate-assets": "node ./scripts/generate-assets.js",
"generate-assets": "node -r ts-node/register ./scripts/generate-assets.ts",
"inline-version": "./scripts/inline-version.js",
"lint.fix": "yarn lint --fix",
"lint": "tsc --noEmit && tslint 'src/**/*.ts'",
Expand All @@ -49,7 +49,7 @@
"prebuild": "yarn build.clean && copyfiles -u 1 \"src/rulesets/oas*/**/*.json\" dist && copyfiles -u 1 \"src/rulesets/oas*/**/*.json\" ./ && yarn copy.html-templates",
"prebuild.binary": "yarn build",
"pretest.karma": "node ./scripts/generate-karma-fixtures.js && yarn pretest",
"pretest": "node ./scripts/generate-assets.js",
"pretest": "yarn generate-assets",
"schema.update": "yarn typescript-json-schema --id \"http://stoplight.io/schemas/rule.schema.json\" --required tsconfig.json IRule --out ./src/meta/rule.schema.json",
"test.harness": "jest -c ./jest.harness.config.js",
"test.karma": "karma start",
Expand Down
61 changes: 0 additions & 61 deletions scripts/generate-assets.js

This file was deleted.

68 changes: 68 additions & 0 deletions scripts/generate-assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This script generates a list of assets that are needed to load spectral:oas ruleset.
* It contains all OAS custom functions and *resolved* rulesets.
* The assets are stores in a single filed call assets.json in the following format:
* `<require-call-path>: <content>`
* where the `require-call-path` is the path you'd normally pass to require(), i.e. `@stoplight/spectral/rulesets/oas/index.js` and `content` is the text data.
* Assets can be loaded using Spectral#registerStaticAssets statc method, i.e. `Spectral.registerStaticAssets(require('@stoplight/spectral/rulesets/assets/assets.json'))`;
* If you execute the code above, ruleset will be loaded fully offline, without a need to make any request.
*/

import { IUriParserResult } from '@stoplight/json-ref-resolver/types';
import * as path from '@stoplight/path';
import { parse } from '@stoplight/yaml';
import * as fs from 'fs';
import { promisify } from 'util';
import { httpAndFileResolver } from '../dist/resolvers/http-and-file';

const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const readdirAsync = promisify(fs.readdir);
const statAsync = promisify(fs.stat);

const baseDir = path.join(__dirname, '../rulesets/assets/');

if (!fs.existsSync(baseDir)) {
fs.mkdirSync(baseDir);
}

const assetsPath = path.join(baseDir, `assets.json`);
const generatedAssets = {};

(async () => {
await processDirectory(generatedAssets, path.join(__dirname, '../rulesets/oas'));
await writeFileAsync(assetsPath, JSON.stringify(generatedAssets, null, 2));
})();

async function processDirectory(assets: Record<string, string>, dir: string) {
await Promise.all(
(await readdirAsync(dir)).map(async (name: string) => {
if (name === 'schemas') return;
const target = path.join(dir, name);
const stats = await statAsync(target);
if (stats.isDirectory()) {
return processDirectory(assets, target);
} else {
let content = await readFileAsync(target, 'utf8');
if (path.extname(name) === '.json') {
content = JSON.stringify(
(
await httpAndFileResolver.resolve(JSON.parse(content), {
dereferenceRemote: true,
dereferenceInline: false,
baseUri: target,
parseResolveResult(opts) {
return new Promise<IUriParserResult>(resolve => {
resolve({ result: parse(opts.result) });
});
},
})
).result,
);
}

assets[path.join('@stoplight/spectral', path.relative(path.join(__dirname, '..'), target))] = content;
}
}),
);
}

0 comments on commit bc75bc9

Please sign in to comment.