From e8e0980307c6fa898d29c16ea8cce41886b66e87 Mon Sep 17 00:00:00 2001 From: Matt Swensen Date: Thu, 7 May 2020 06:57:36 -0600 Subject: [PATCH] Implement Windows Terminal theme generator --- cli/packages/windows-terminal/.gitignore | 2 + cli/packages/windows-terminal/.yarnrc | 2 + cli/packages/windows-terminal/README.md | 15 ++++ .../lib/__snapshots__/index.spec.js.snap | 79 ++++++++++++++++++ cli/packages/windows-terminal/lib/index.js | 82 +++++++++++++++++++ .../windows-terminal/lib/index.spec.js | 22 +++++ cli/packages/windows-terminal/package.json | 35 ++++++++ 7 files changed, 237 insertions(+) create mode 100644 cli/packages/windows-terminal/.gitignore create mode 100644 cli/packages/windows-terminal/.yarnrc create mode 100644 cli/packages/windows-terminal/README.md create mode 100644 cli/packages/windows-terminal/lib/__snapshots__/index.spec.js.snap create mode 100644 cli/packages/windows-terminal/lib/index.js create mode 100644 cli/packages/windows-terminal/lib/index.spec.js create mode 100644 cli/packages/windows-terminal/package.json diff --git a/cli/packages/windows-terminal/.gitignore b/cli/packages/windows-terminal/.gitignore new file mode 100644 index 0000000..4f1b7d8 --- /dev/null +++ b/cli/packages/windows-terminal/.gitignore @@ -0,0 +1,2 @@ +node_modules +LICENSE.md diff --git a/cli/packages/windows-terminal/.yarnrc b/cli/packages/windows-terminal/.yarnrc new file mode 100644 index 0000000..4117473 --- /dev/null +++ b/cli/packages/windows-terminal/.yarnrc @@ -0,0 +1,2 @@ +version-tag-prefix "@themer/windows-terminal-v" +version-git-message "@themer/windows-terminal-v%s" diff --git a/cli/packages/windows-terminal/README.md b/cli/packages/windows-terminal/README.md new file mode 100644 index 0000000..1cadb62 --- /dev/null +++ b/cli/packages/windows-terminal/README.md @@ -0,0 +1,15 @@ +# @themer/windows-terminal + +A [Windows Terminal](https://github.com/microsoft/terminal) template for [themer](https://github.com/mjswensen/themer). + +## Installation & usage + +Install this module wherever you have `themer` installed: + + npm install @themer/windows-terminal + +Then pass `@themer/windows-terminal` as a `-t` (`--template`) arg to `themer`: + + themer -c my-colors.js -t @themer/windows-terminal -o gen + +Installation instructions for the generated theme(s) will be included in `/README.md`. diff --git a/cli/packages/windows-terminal/lib/__snapshots__/index.spec.js.snap b/cli/packages/windows-terminal/lib/__snapshots__/index.spec.js.snap new file mode 100644 index 0000000..af23369 --- /dev/null +++ b/cli/packages/windows-terminal/lib/__snapshots__/index.spec.js.snap @@ -0,0 +1,79 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`themer Windows Terminal theme generator should generate well-formatted themes 1`] = ` +"{ + \\"name\\": \\"Themer Dark\\", + \\"background\\": \\"#282629\\", + \\"foreground\\": \\"#E0DCE0\\", + \\"cursorColor\\": \\"#CC78FA\\", + \\"selectionBackground\\": \\"#66BFFF\\", + \\"black\\": \\"#656066\\", + \\"brightBlack\\": \\"#847E85\\", + \\"red\\": \\"#FF4050\\", + \\"brightRed\\": \\"#FF6673\\", + \\"green\\": \\"#A4CC35\\", + \\"brightGreen\\": \\"#B6D65D\\", + \\"brightYellow\\": \\"#FFDA6E\\", + \\"blue\\": \\"#66BFFF\\", + \\"brightBlue\\": \\"#85CBFF\\", + \\"purple\\": \\"#CC78FA\\", + \\"brightPurple\\": \\"#D692FB\\", + \\"cyan\\": \\"#26C99E\\", + \\"brightCyan\\": \\"#51D3B1\\", + \\"white\\": \\"#E0DCE0\\", + \\"brightWhite\\": \\"#FFFCFF\\" +}" +`; + +exports[`themer Windows Terminal theme generator should generate well-formatted themes 2`] = ` +"{ + \\"name\\": \\"Themer Light\\", + \\"background\\": \\"#FFFCFF\\", + \\"foreground\\": \\"#474247\\", + \\"cursorColor\\": \\"#BF65F0\\", + \\"selectionBackground\\": \\"#53A6E1\\", + \\"black\\": \\"#474247\\", + \\"brightBlack\\": \\"#656066\\", + \\"red\\": \\"#F03E4D\\", + \\"brightRed\\": \\"#F36471\\", + \\"green\\": \\"#97BD2D\\", + \\"brightGreen\\": \\"#ACCA57\\", + \\"brightYellow\\": \\"#F1C74D\\", + \\"blue\\": \\"#53A6E1\\", + \\"brightBlue\\": \\"#75B7E7\\", + \\"purple\\": \\"#BF65F0\\", + \\"brightPurple\\": \\"#CC83F3\\", + \\"cyan\\": \\"#1FC598\\", + \\"brightCyan\\": \\"#4CD0AD\\", + \\"white\\": \\"#C1BCC2\\", + \\"brightWhite\\": \\"#E0DCE0\\" +}" +`; + +exports[`themer Windows Terminal theme generator should provide installation instructions 1`] = ` +" +1. Open the Windows Terminal settings (\`Ctrl\`-\`,\`) +2. Add the contents of 'themer-dark.json' and 'themer-light.json' to the \`schemes\` array in \`profile.json\` +3. Set the \`colorScheme\` property to the desired scheme name (\\"Themer Dark\\" or \\"Themer Light\\") in the profiles section of \`profile.json\`, e.g.: + + \\"profiles\\": { + \\"defaults\\": { + \\"colorScheme\\": \\"Themer Dark\\" + } + } +" +`; + +exports[`themer Windows Terminal theme generator should provide installation instructions 2`] = ` +" +1. Open the Windows Terminal settings (\`Ctrl\`-\`,\`) +2. Add the contents of 'themer-light.json' to the \`schemes\` array in \`profile.json\` +3. Set the \`colorScheme\` property to the desired scheme name (\\"Themer Light\\") in the profiles section of \`profile.json\`, e.g.: + + \\"profiles\\": { + \\"defaults\\": { + \\"colorScheme\\": \\"Themer Light\\" + } + } +" +`; diff --git a/cli/packages/windows-terminal/lib/index.js b/cli/packages/windows-terminal/lib/index.js new file mode 100644 index 0000000..47fb73a --- /dev/null +++ b/cli/packages/windows-terminal/lib/index.js @@ -0,0 +1,82 @@ +const path = require('path'), + Color = require('color'); + +const MIX = 0.2; + +const brightMix = (colors, key, isDark) => + Color(colors[key]) + .mix(isDark ? Color(colors.shade7) : Color(colors.shade0), MIX) + .hex(); + +const renderTheme = ([firstLetter, ...letters], colors, isDark) => JSON.stringify( + { + name: `Themer ${[firstLetter.toUpperCase(), ...letters].join('')}`, + + background: colors.shade0, + foreground: colors.shade6, + + cursorColor: colors.accent6, + selectionBackground: colors.accent5, + + black: isDark ? colors.shade2 : colors.shade6, + brightBlack: isDark ? colors.shade3 : colors.shade5, + + red: colors.accent0, + brightRed: brightMix(colors, 'accent0', isDark), + + green: colors.accent3, + brightGreen: brightMix(colors, 'accent3', isDark), + + yellow: colors.accen2, + brightYellow: brightMix(colors, 'accent2', isDark), + + blue: colors.accent5, + brightBlue: brightMix(colors, 'accent5', isDark), + + purple: colors.accent6, + brightPurple: brightMix(colors, 'accent6', isDark), + + cyan: colors.accent4, + brightCyan: brightMix(colors, 'accent4', isDark), + + white: isDark ? colors.shade6 : colors.shade2, + brightWhite: isDark ? colors.shade7 : colors.shade1, + }, + null, + 2, +); + +const render = colors => Object.entries(colors) + .map(async ([name, colors]) => ({ + name: `themer-${name}.json`, + contents: Buffer.from(renderTheme(name, colors, name === 'dark')), + })); + +const pathsToThemeNames = paths => + paths + .map(p => path.basename(p, '.json')) + .map(basename => + basename + .split('-') + .map(([firstLetter, ...tail]) => + [firstLetter.toUpperCase(), ...tail].join(''), + ) + .join(' '), + ); + +const renderInstructions = paths => ` +1. Open the Windows Terminal settings (\`Ctrl\`-\`,\`) +2. Add the contents of ${paths.map(p => `'${p}'`).join(' and ')} to the \`schemes\` array in \`profile.json\` +3. Set the \`colorScheme\` property to the desired scheme name (${pathsToThemeNames(paths).map(name => `"${name}"`).join(' or ')}) in the profiles section of \`profile.json\`, e.g.: + + "profiles": { + "defaults": { + "colorScheme": "${pathsToThemeNames(paths)[0]}" + } + } +`; + +module.exports = { + render, + renderInstructions, +}; diff --git a/cli/packages/windows-terminal/lib/index.spec.js b/cli/packages/windows-terminal/lib/index.spec.js new file mode 100644 index 0000000..b977b73 --- /dev/null +++ b/cli/packages/windows-terminal/lib/index.spec.js @@ -0,0 +1,22 @@ +const { render, renderInstructions } = require('./index'); +const { colors } = require('../../colors-default'); + +describe('themer Windows Terminal theme generator', () => { + const promisedFiles = Promise.all(render(colors)); + + it('should generate well-formatted themes', async () => { + const files = await promisedFiles; + for (const file of files) { + expect(file.contents.toString('utf8')).toMatchSnapshot(); + } + }); + + it('should provide installation instructions', async () => { + const files = await promisedFiles; + const instructions = renderInstructions(files.map(({ name }) => name)); + expect(instructions).toMatchSnapshot(); + + const singleThemeInstructions = renderInstructions([files[1].name]); + expect(singleThemeInstructions).toMatchSnapshot(); + }); +}); diff --git a/cli/packages/windows-terminal/package.json b/cli/packages/windows-terminal/package.json new file mode 100644 index 0000000..147f331 --- /dev/null +++ b/cli/packages/windows-terminal/package.json @@ -0,0 +1,35 @@ +{ + "name": "@themer/windows-terminal", + "version": "1.0.0", + "description": "TODO", + "main": "lib/index.js", + "engines": { + "node": ">=8.11.4" + }, + "scripts": { + "prepublishOnly": "cp ../../../LICENSE.md ./" + }, + "author": "mjswensen", + "license": "MIT", + "files": [ + "/lib/index.js" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/mjswensen/themer.git" + }, + "bugs": { + "url": "https://github.com/mjswensen/themer/issues" + }, + "homepage": "https://github.com/mjswensen/themer/tree/master/cli/packages/windows-terminal#readme", + "peerDependencies": { + "themer": "^3" + }, + "keywords": [ + "themer", + "windows terminal" + ], + "dependencies": { + "color": "^3.1.2" + } +}