|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import postcss from 'postcss'; |
| 4 | +import getCustomProperties from './get-custom-properties'; |
| 5 | + |
| 6 | +/* Import Custom Properties from CSS AST |
| 7 | +/* ========================================================================== */ |
| 8 | + |
| 9 | +function importCustomPropertiesFromCSSAST(root) { |
| 10 | + return getCustomProperties(root, { preserve: true }); |
| 11 | +} |
| 12 | + |
| 13 | +/* Import Custom Properties from CSS File |
| 14 | +/* ========================================================================== */ |
| 15 | + |
| 16 | +async function importCustomPropertiesFromCSSFile(from) { |
| 17 | + const css = await readFile(path.resolve(from)); |
| 18 | + const root = postcss.parse(css, { from: path.resolve(from) }); |
| 19 | + |
| 20 | + return importCustomPropertiesFromCSSAST(root); |
| 21 | +} |
| 22 | + |
| 23 | +/* Import Custom Properties from Object |
| 24 | +/* ========================================================================== */ |
| 25 | + |
| 26 | +function importCustomPropertiesFromObject(object) { |
| 27 | + const customProperties = Object.assign( |
| 28 | + {}, |
| 29 | + Object(object).customProperties || Object(object)['custom-properties'] |
| 30 | + ); |
| 31 | + |
| 32 | + return customProperties; |
| 33 | +} |
| 34 | + |
| 35 | +/* Import Custom Properties from JSON file |
| 36 | +/* ========================================================================== */ |
| 37 | + |
| 38 | +async function importCustomPropertiesFromJSONFile(from) { |
| 39 | + const object = await readJSON(path.resolve(from)); |
| 40 | + |
| 41 | + return importCustomPropertiesFromObject(object); |
| 42 | +} |
| 43 | + |
| 44 | +/* Import Custom Properties from JS file |
| 45 | +/* ========================================================================== */ |
| 46 | + |
| 47 | +async function importCustomPropertiesFromJSFile(from) { |
| 48 | + const object = await import(path.resolve(from)); |
| 49 | + |
| 50 | + return importCustomPropertiesFromObject(object); |
| 51 | +} |
| 52 | + |
| 53 | +/* Import Custom Properties from Sources |
| 54 | +/* ========================================================================== */ |
| 55 | + |
| 56 | +export default function importCustomPropertiesFromSources(sources) { |
| 57 | + return sources.map(source => { |
| 58 | + if (source instanceof Promise) { |
| 59 | + return source; |
| 60 | + } else if (source instanceof Function) { |
| 61 | + return source(); |
| 62 | + } |
| 63 | + |
| 64 | + // read the source as an object |
| 65 | + const opts = source === Object(source) ? source : { from: String(source) }; |
| 66 | + |
| 67 | + // skip objects with Custom Properties |
| 68 | + if (opts.customProperties || opts['custom-properties']) { |
| 69 | + return opts |
| 70 | + } |
| 71 | + |
| 72 | + // source pathname |
| 73 | + const from = String(opts.from || ''); |
| 74 | + |
| 75 | + // type of file being read from |
| 76 | + const type = (opts.type || path.extname(from).slice(1)).toLowerCase(); |
| 77 | + |
| 78 | + return { type, from }; |
| 79 | + }).reduce(async (customProperties, source) => { |
| 80 | + const { type, from } = await source; |
| 81 | + |
| 82 | + if (type === 'ast') { |
| 83 | + return Object.assign(await customProperties, importCustomPropertiesFromCSSAST(from)); |
| 84 | + } |
| 85 | + |
| 86 | + if (type === 'css') { |
| 87 | + return Object.assign(await customProperties, await importCustomPropertiesFromCSSFile(from)); |
| 88 | + } |
| 89 | + |
| 90 | + if (type === 'js') { |
| 91 | + return Object.assign(await customProperties, await importCustomPropertiesFromJSFile(from)); |
| 92 | + } |
| 93 | + |
| 94 | + if (type === 'json') { |
| 95 | + return Object.assign(await customProperties, await importCustomPropertiesFromJSONFile(from)); |
| 96 | + } |
| 97 | + |
| 98 | + return Object.assign(await customProperties, await importCustomPropertiesFromObject(await source)); |
| 99 | + }, {}); |
| 100 | +} |
| 101 | + |
| 102 | +/* Helper utilities |
| 103 | +/* ========================================================================== */ |
| 104 | + |
| 105 | +const readFile = from => new Promise((resolve, reject) => { |
| 106 | + fs.readFile(from, 'utf8', (error, result) => { |
| 107 | + if (error) { |
| 108 | + reject(error); |
| 109 | + } else { |
| 110 | + resolve(result); |
| 111 | + } |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +const readJSON = async from => JSON.parse(await readFile(from)); |
0 commit comments