Skip to content

Commit

Permalink
Refactor build script and improve lint script (dracula#90)
Browse files Browse the repository at this point in the history
* Refactor build script and read theme from source

* Fix 'soft' typo

* Reduce complexity at getSoftThemeYAML function

* Fix imports order. Remove catch from build
  • Loading branch information
DanielRamosAcosta authored and dsifford committed Oct 29, 2018
1 parent afa9d81 commit bf3c10d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 74 deletions.
52 changes: 0 additions & 52 deletions build.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -8,7 +8,8 @@
"license": "MIT",
"scripts": {
"attach": "./bootstrap.sh attach",
"build": "node ./build.js",
"build": "node ./scripts/build.js",
"lint": "node ./scripts/lint.js",
"eject": "./bootstrap.sh eject",
"package": "vsce package -o ./bin/dracula.vsix",
"vscode:prepublish": "npm run build"
Expand Down
27 changes: 27 additions & 0 deletions scripts/build.js
@@ -0,0 +1,27 @@
'use strict';

const path = require('path');
const fsp = require('./fsp');
const loadThemes = require('./loadThemes');

const THEME_DIR = path.join(__dirname, '..', 'theme');
const THEME_YAML_FILE = path.join(__dirname, '..', 'src', 'dracula.yml');

function toJSON(theme) {
return JSON.stringify(theme, null, 4);
}

(async () => {
if (!(await fsp.exists(THEME_DIR))) {
await fsp.mkdir(THEME_DIR);
}

const { standardTheme, softTheme } = await loadThemes(THEME_YAML_FILE);
const standardThemePath = path.join(THEME_DIR, 'dracula.json');
const softThemePath = path.join(THEME_DIR, 'dracula-soft.json');

await Promise.all([
fsp.writeFile(standardThemePath, toJSON(standardTheme)),
fsp.writeFile(softThemePath, toJSON(softTheme)),
]);
})();
11 changes: 11 additions & 0 deletions scripts/fsp.js
@@ -0,0 +1,11 @@
'use strict';

const fs = require('fs');
const util = require('util');

module.exports = {
exists: util.promisify(fs.exists),
mkdir: util.promisify(fs.mkdir),
readFile: util.promisify(fs.readFile),
writeFile: util.promisify(fs.writeFile),
};
29 changes: 8 additions & 21 deletions lint.js → scripts/lint.js
@@ -1,8 +1,8 @@
'use strict';

const https = require('https');
const fs = require('fs');
const util = require('util');
const path = require('path');
const loadThemes = require('./loadThemes');

const get = url =>
new Promise((resolve, reject) => {
Expand All @@ -15,8 +15,6 @@ const get = url =>
});
});

const readFile = util.promisify(fs.readFile);

const THEME_COLOR_REFERENCE_URL =
'https://code.visualstudio.com/docs/getstarted/theme-color-reference';

Expand All @@ -25,6 +23,8 @@ const NOT_THEME_KEYS = [
'editor.tokenColorCustomizations',
];

const THEME_YAML_FILE = path.join(__dirname, '..', 'src', 'dracula.yml');

async function scrapeThemeAvailableKeys() {
const data = await get(THEME_COLOR_REFERENCE_URL);

Expand All @@ -47,32 +47,19 @@ async function scrapeThemeAvailableKeys() {
return availableKeys;
}

async function getTheme(fileToLint) {
const fileContent = await readFile(fileToLint, 'utf8');
const theme = JSON.parse(fileContent);
return theme;
}

(async () => {
const fileToLint = process.argv[2];

if (!fileToLint) {
throw new Error('You need to specify the path of the theme to lint');
}

const [availableKeys, theme] = await Promise.all([
const [availableKeys, { standardTheme }] = await Promise.all([
scrapeThemeAvailableKeys(),
getTheme(fileToLint),
loadThemes(THEME_YAML_FILE),
]);

Object.keys(theme.colors).forEach(key => {
Object.keys(standardTheme.colors).forEach(key => {
if (!availableKeys.includes(key)) {
console.warn(`Unsupported key "${key}", probably deprecated?`);
}
});

availableKeys.forEach(key => {
if (!Object.keys(theme.colors).includes(key)) {
if (!Object.keys(standardTheme.colors).includes(key)) {
console.warn(`Missing key "${key}" in theme`);
}
});
Expand Down
33 changes: 33 additions & 0 deletions scripts/loadThemes.js
@@ -0,0 +1,33 @@
'use strict';

const tinycolor = require('tinycolor2');
const fsp = require('./fsp');
const { loadYAML } = require('./yaml');

async function loadTheme(yamlFilePath) {
const standardThemeYAML = await fsp.readFile(yamlFilePath, 'utf8');
const standardTheme = await loadYAML(standardThemeYAML);

const softThemeYAML = getSoftThemeYAML(standardThemeYAML, standardTheme);
const softTheme = await loadYAML(softThemeYAML);

return { standardTheme, softTheme };
}

function getSoftThemeYAML(fileContent, standardTheme) {
const brightColors = [
...standardTheme.dracula.ansi,
...standardTheme.dracula.brightOther,
];

return fileContent.replace(/#[0-9A-F]{6}/g, color => {
if (brightColors.includes(color)) {
return tinycolor(color)
.desaturate(20)
.toHexString();
}
return color;
});
}

module.exports = loadTheme;
19 changes: 19 additions & 0 deletions scripts/yaml.js
@@ -0,0 +1,19 @@
'use strict';

const { Type, Schema, load } = require('js-yaml');

const withAlphaType = new Type('!alpha', {
kind: 'sequence',
construct: ([hexRGB, alpha]) => hexRGB + alpha,
represent: ([hexRGB, alpha]) => hexRGB + alpha,
});

const schema = Schema.create([withAlphaType]);

async function loadYAML(file) {
return load(file, { schema });
}

module.exports = {
loadYAML,
};

0 comments on commit bf3c10d

Please sign in to comment.