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