-
-
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
18 changed files
with
407 additions
and
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/.nyc_output | ||
/dist | ||
/lib | ||
/morfeo | ||
/test/builds | ||
/tmp | ||
/yarn.lock | ||
node_modules |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { run } from '@oclif/command'; | ||
export * from './types'; |
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,24 @@ | ||
export type BuildConfig = { | ||
/** | ||
* an identifier for the passed theme, for example "light", "dark" | ||
*/ | ||
name: string; | ||
/** | ||
* the path where the generated css files will be placed | ||
* @default `morfeo` | ||
*/ | ||
buildPath?: string; | ||
/** | ||
* the path to the configuration file. | ||
* valid extensions are `js`, `ts`, `json` or `no extension` | ||
* @default `./.morfeorc` | ||
*/ | ||
configPath?: string; | ||
}; | ||
|
||
export type MorfeoConfig = { | ||
/** | ||
* the path where the generated css files will be placed | ||
*/ | ||
buildPath: string; | ||
}; |
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 @@ | ||
export * from './config'; |
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,66 @@ | ||
import { theme, ThemeKey } from '@morfeo/web'; | ||
import * as path from 'path'; | ||
import { SLICES_TO_BE_EXCLUDED } from '../constants'; | ||
import { getCSSClasses } from './getCSSClasses'; | ||
import { safeWrite } from './safeWrite'; | ||
import { parseSlice } from './parseSlice'; | ||
import { BuildConfig } from '../types'; | ||
import { getConfiguration } from './getConfiguration'; | ||
|
||
function getStylePaths(buildPath: string) { | ||
const variablesPath = path.join(process.cwd(), buildPath, 'variables.css'); | ||
const stylePath = path.join(process.cwd(), buildPath, 'style.css'); | ||
|
||
return { | ||
variablesPath, | ||
stylePath, | ||
}; | ||
} | ||
|
||
function wrapWithScope(name: string, css: string) { | ||
const parsedCss = css.replace(/\n/g, '\n\t'); | ||
return `:root, html[data-morfeo-theme="${name}"] {\n\t${parsedCss}\n}\n`; | ||
} | ||
|
||
export function buildTheme(config: BuildConfig) { | ||
const { name, buildPath } = getConfiguration(config); | ||
|
||
const currentTheme = theme.get(); | ||
const slices = Object.keys(currentTheme) as ThemeKey[]; | ||
const filtered = slices.filter( | ||
slice => !SLICES_TO_BE_EXCLUDED.includes(slice), | ||
); | ||
let cssText = ''; | ||
|
||
// `breakpoints` excluded since is not possible to have variables in media queries | ||
const { breakpoints, ...newTheme } = filtered.reduce((acc, curr) => { | ||
const { css, object } = parseSlice(curr); | ||
cssText += css; | ||
return { | ||
...acc, | ||
[curr]: object, | ||
}; | ||
}, currentTheme); | ||
|
||
const { stylePath, variablesPath } = getStylePaths(buildPath as string); | ||
|
||
safeWrite(variablesPath, wrapWithScope(name, cssText)); | ||
|
||
/** | ||
* Setting the new theme where values of slices are css variables | ||
*/ | ||
theme.set(newTheme); | ||
|
||
/** | ||
* getCSSClasses will return a css class for each component and foreach variant using | ||
* the new theme with css variables as values | ||
*/ | ||
const componentStyle = getCSSClasses(); | ||
|
||
safeWrite(stylePath, componentStyle); | ||
|
||
/** | ||
* restored the old theme | ||
*/ | ||
theme.set(currentTheme); | ||
} |
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,29 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { BuildConfig, MorfeoConfig } from '../types'; | ||
|
||
const MORFEO_CONFIG_FILE_EXTENSIONS = ['.js', '.ts', '.json', '']; | ||
|
||
export function getConfiguration({ | ||
configPath, | ||
...rest | ||
}: Partial<BuildConfig>): BuildConfig { | ||
const morfeoConfigPath = path.join(process.cwd(), configPath as string); | ||
let morfeoConfig: MorfeoConfig = {} as any; | ||
const configExists = MORFEO_CONFIG_FILE_EXTENSIONS.some(extension => { | ||
return fs.existsSync(`${morfeoConfigPath}${extension}`); | ||
}); | ||
|
||
if (configExists) { | ||
const imported = require(morfeoConfigPath); | ||
morfeoConfig = imported.default ? imported.default : imported; | ||
} | ||
|
||
const { buildPath } = morfeoConfig; | ||
|
||
return { | ||
...rest, | ||
buildPath: rest.buildPath || buildPath, | ||
configPath, | ||
} as BuildConfig; | ||
} |
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 @@ | ||
export * from './parseTheme'; | ||
export * from './buildTheme'; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.