mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-05 02:20:10 +00:00
refactor: colorSchemes
This commit is contained in:
parent
2269c19169
commit
0d62e76cd6
2 changed files with 44 additions and 34 deletions
|
@ -1,7 +1,7 @@
|
||||||
import * as fs from "fs"
|
import * as fs from "fs"
|
||||||
import { tmpdir } from "os"
|
import { tmpdir } from "os"
|
||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
import colorSchemes, { staffColorSchemes } from "./colorSchemes"
|
import { colorSchemes, staffColorSchemes } from "./colorSchemes"
|
||||||
import app from "./styleTree/app"
|
import app from "./styleTree/app"
|
||||||
import { ColorScheme } from "./themes/common/colorScheme"
|
import { ColorScheme } from "./themes/common/colorScheme"
|
||||||
import snakeCase from "./utils/snakeCase"
|
import snakeCase from "./utils/snakeCase"
|
||||||
|
|
|
@ -2,53 +2,63 @@ import fs from "fs"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { ColorScheme, Meta } from "./themes/common/colorScheme"
|
import { ColorScheme, Meta } from "./themes/common/colorScheme"
|
||||||
|
|
||||||
const colorSchemes: ColorScheme[] = []
|
const THEMES_DIRECTORY = path.resolve(`${__dirname}/themes`)
|
||||||
export default colorSchemes
|
const STAFF_DIRECTORY = path.resolve(`${__dirname}/themes/staff`)
|
||||||
|
const IGNORE_ITEMS = ["staff", "common", "template.ts"]
|
||||||
|
const ACCEPT_EXTENSION = ".ts"
|
||||||
|
|
||||||
const schemeMeta: Meta[] = []
|
function getAllTsFiles(directoryPath: string) {
|
||||||
export { schemeMeta }
|
const files = fs.readdirSync(directoryPath)
|
||||||
|
const fileList: string[] = []
|
||||||
|
|
||||||
const staffColorSchemes: ColorScheme[] = []
|
for (const file of files) {
|
||||||
export { staffColorSchemes }
|
if (!IGNORE_ITEMS.includes(file)) {
|
||||||
|
const filePath = path.join(directoryPath, file)
|
||||||
|
|
||||||
const experimentalColorSchemes: ColorScheme[] = []
|
if (fs.statSync(filePath).isDirectory()) {
|
||||||
export { experimentalColorSchemes }
|
fileList.push(...getAllTsFiles(filePath))
|
||||||
|
} else if (path.extname(file) === ACCEPT_EXTENSION) {
|
||||||
const themes_directory = path.resolve(`${__dirname}/themes`)
|
fileList.push(filePath)
|
||||||
|
}
|
||||||
function for_all_color_schemes_in(
|
|
||||||
themesPath: string,
|
|
||||||
callback: (module: any, path: string) => void
|
|
||||||
) {
|
|
||||||
for (const fileName of fs.readdirSync(themesPath)) {
|
|
||||||
if (fileName == "template.ts") continue
|
|
||||||
const filePath = path.join(themesPath, fileName)
|
|
||||||
|
|
||||||
if (fs.statSync(filePath).isFile()) {
|
|
||||||
const colorScheme = require(filePath)
|
|
||||||
callback(colorScheme, path.basename(filePath))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fileList
|
||||||
}
|
}
|
||||||
|
|
||||||
function fillColorSchemes(themesPath: string, colorSchemes: ColorScheme[]) {
|
function getAllColorSchemes(directoryPath: string) {
|
||||||
for_all_color_schemes_in(themesPath, (colorScheme, _path) => {
|
const files = getAllTsFiles(directoryPath)
|
||||||
|
return files.map((filePath) => ({
|
||||||
|
colorScheme: require(filePath),
|
||||||
|
filePath: path.basename(filePath),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
function getColorSchemes(directoryPath: string) {
|
||||||
|
const colorSchemes: ColorScheme[] = []
|
||||||
|
|
||||||
|
for (const { colorScheme } of getAllColorSchemes(directoryPath)) {
|
||||||
if (colorScheme.dark) colorSchemes.push(colorScheme.dark)
|
if (colorScheme.dark) colorSchemes.push(colorScheme.dark)
|
||||||
if (colorScheme.light) colorSchemes.push(colorScheme.light)
|
else if (colorScheme.light) colorSchemes.push(colorScheme.light)
|
||||||
})
|
}
|
||||||
|
|
||||||
|
return colorSchemes
|
||||||
}
|
}
|
||||||
|
|
||||||
fillColorSchemes(themes_directory, colorSchemes)
|
function getMeta(directoryPath: string) {
|
||||||
fillColorSchemes(path.resolve(`${themes_directory}/staff`), staffColorSchemes)
|
const meta: Meta[] = []
|
||||||
|
|
||||||
function fillMeta(themesPath: string, meta: Meta[]) {
|
for (const { colorScheme, filePath } of getAllColorSchemes(directoryPath)) {
|
||||||
for_all_color_schemes_in(themesPath, (colorScheme, path) => {
|
|
||||||
if (colorScheme.meta) {
|
if (colorScheme.meta) {
|
||||||
meta.push(colorScheme.meta)
|
meta.push(colorScheme.meta)
|
||||||
} else {
|
} else {
|
||||||
throw Error(`Public theme ${path} must have a meta field`)
|
throw Error(`Public theme ${filePath} must have a meta field`)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
return meta
|
||||||
}
|
}
|
||||||
|
|
||||||
fillMeta(themes_directory, schemeMeta)
|
export const colorSchemes = getColorSchemes(THEMES_DIRECTORY)
|
||||||
|
export const staffColorSchemes = getColorSchemes(STAFF_DIRECTORY)
|
||||||
|
export const schemeMeta = getMeta(THEMES_DIRECTORY)
|
||||||
|
|
Loading…
Reference in a new issue