-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
540 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Command, flags } from '@oclif/command'; | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
import { | ||
watchFiles, | ||
composeSlice, | ||
updateThemes, | ||
getConfiguration, | ||
} from '../utils'; | ||
|
||
export default class Compose extends Command { | ||
static description = 'compose morfeo style files into a single theme'; | ||
|
||
static examples = [`$ morfeo compose`, `$ morfeo compose --watch`]; | ||
|
||
static usage = 'build'; | ||
|
||
static flags = { | ||
help: flags.help({ char: 'h', description: Compose.description }), | ||
watch: flags.boolean({ | ||
char: 'w', | ||
description: 'watch file changes', | ||
default: false, | ||
required: false, | ||
}), | ||
config: flags.string({ | ||
char: 'c', | ||
description: 'the path to the configuration file', | ||
default: '.morfeorc', | ||
required: false, | ||
}), | ||
}; | ||
|
||
static args = []; | ||
|
||
async run() { | ||
const { flags } = this.parse(Compose); | ||
const { config } = flags; | ||
const { themes } = getConfiguration(config); | ||
|
||
const files = glob | ||
.sync('**/*.morfeo.{ts,js}', {}) | ||
.map(file => path.join(process.cwd(), file)); | ||
|
||
if (files.length === 0) { | ||
this.error('No *.morfeo.{ts|js} files found'); | ||
} | ||
|
||
const compose = (list: string[]) => { | ||
console.clear(); | ||
console.log(` - ${list.join(',\n - ')}`); | ||
const mergedThemes = composeSlice(themes || {}, list); | ||
updateThemes(themes || {}, mergedThemes); | ||
console.log(`\n\n\n\n^ + C to exit`); | ||
}; | ||
|
||
if (flags.watch) { | ||
watchFiles(files, (file: string) => { | ||
compose([file]); | ||
}); | ||
} | ||
|
||
compose(files); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Theme, ThemeKey } from '@morfeo/web'; | ||
import * as path from 'path'; | ||
|
||
function resolvePath(filePath: string) { | ||
return path.join(process.cwd(), filePath); | ||
} | ||
|
||
const themeKeys: ThemeKey[] = [ | ||
'sizes', | ||
'radii', | ||
'colors', | ||
'borders', | ||
'shadows', | ||
'zIndices', | ||
'spacings', | ||
'opacities', | ||
'fontSizes', | ||
'transitions', | ||
'breakpoints', | ||
'lineHeights', | ||
'fontWeights', | ||
'borderWidths', | ||
'borderStyles', | ||
'mediaQueries', | ||
'letterSpacings', | ||
]; | ||
|
||
export function composeSlice(themes: Record<string, string>, files: string[]) { | ||
const objects = files.map(file => { | ||
delete require.cache[file]; | ||
const [defaultName] = path.basename(file).split('.') as ThemeKey[]; | ||
const { | ||
default: value, | ||
theme, | ||
sliceName = defaultName, | ||
componentName = defaultName, | ||
} = require(file); | ||
|
||
if (themeKeys.includes(defaultName) || themeKeys.includes(sliceName)) { | ||
return { value, theme, sliceName }; | ||
} | ||
|
||
return { value, theme, componentName }; | ||
}); | ||
|
||
const themeNames = Object.keys(themes); | ||
let themeObjects = themeNames.reduce((acc, themeName) => { | ||
const required = require(resolvePath(themes[themeName])); | ||
const currentTheme = required.default ? required.default : required; | ||
|
||
return { | ||
...acc, | ||
[themeName]: currentTheme, | ||
}; | ||
}, {}); | ||
|
||
objects.forEach(({ value, theme, sliceName, componentName }) => { | ||
const filteredThemeNames = theme ? [theme] : themeNames; | ||
|
||
filteredThemeNames.forEach(themeName => { | ||
themeObjects[themeName] = { | ||
...themeObjects[themeName], | ||
...(sliceName ? { [sliceName]: value } : {}), | ||
components: { | ||
...themeObjects[themeName].components, | ||
...(componentName ? { [componentName]: value } : {}), | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
||
return themeObjects as Record<string, Theme>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export * from './watchFiles'; | ||
export * from './buildTheme'; | ||
export * from './updateThemes'; | ||
export * from './composeSlice'; | ||
export * from './getConfiguration'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Theme } from '@morfeo/web'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export function updateThemes<Key extends string>( | ||
themes: Record<Key, string>, | ||
themeObjects: Record<Key, Theme>, | ||
) { | ||
const themeNames = Object.keys(themes) as Key[]; | ||
|
||
function makeThemeFile(themePath: string, themeObject: Theme) { | ||
const stringified = JSON.stringify(themeObject, undefined, 2); | ||
const ext = path.extname(themePath); | ||
if (ext.includes('json')) { | ||
return stringified; | ||
} | ||
|
||
if (ext.includes('js')) { | ||
return `module.exports = ${stringified}`; | ||
} | ||
|
||
return `export default ${stringified}`; | ||
} | ||
|
||
themeNames.forEach(themeName => { | ||
fs.writeFileSync( | ||
path.join(process.cwd(), themes[themeName]), | ||
makeThemeFile(themes[themeName], themeObjects[themeName]), | ||
); | ||
}); | ||
} |
Oops, something went wrong.