-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Convert resolveTailwindConfig.js to ESM (#108)
* Convert resolve script to ESM * Fix missing config default key * Clean up resolveTailwindConfig.js * Remove async statement on parseConfig fn
Showing
1 changed file
with
27 additions
and
22 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 |
---|---|---|
@@ -1,38 +1,43 @@ | ||
#!/usr/bin/env node | ||
const fs = require('fs'); | ||
const TEMP_DIR = './tmp'; | ||
const OUTPUT_PATH = `${TEMP_DIR}/tailwind.config.php`; | ||
|
||
try { | ||
const resolveConfig = require('tailwindcss/resolveConfig'); | ||
const config = require(process.env.CONFIGPATH); | ||
const tempDir = './tmp'; | ||
const outputPath = `${tempDir}/tailwind.config.php`; | ||
const fullConfig = resolveConfig(config); | ||
function parseConfig(data) { | ||
let output = ''; | ||
|
||
function parseConfig(data) { | ||
let output = ''; | ||
for (const [key, item] of Object.entries(data)) { | ||
const value = item == null || typeof item === 'function' ? null : item; | ||
const str = | ||
value && typeof value === 'object' | ||
? parseConfig(value) | ||
: JSON.stringify(value); | ||
|
||
for (const [key, item] of Object.entries(data)) { | ||
const value = item == null || typeof item === 'function' ? null : item; | ||
const str = | ||
value && typeof value === 'object' | ||
? parseConfig(value) | ||
: JSON.stringify(value); | ||
output += `'${key}' => ${str},`; | ||
} | ||
|
||
output += `'${key}' => ${str},`; | ||
} | ||
return `[${output}]`; | ||
} | ||
|
||
return `[${output}]`; | ||
} | ||
async function resolveTailwindConfig() { | ||
const fs = await import('fs'); | ||
const { default: resolveConfig } = await import('tailwindcss/resolveConfig.js') | ||
const { default: config } = await import(process.env.CONFIGPATH) | ||
|
||
const fullConfig = resolveConfig(config); | ||
|
||
try { | ||
if (!fs.existsSync(tempDir)) { | ||
fs.mkdirSync(tempDir); | ||
if (!fs.existsSync(TEMP_DIR)) { | ||
fs.mkdirSync(TEMP_DIR); | ||
} | ||
|
||
fs.writeFileSync(outputPath, `<?php return ${parseConfig(fullConfig)};`); | ||
fs.writeFileSync(OUTPUT_PATH, `<?php return ${parseConfig(fullConfig)};`); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
try { | ||
resolveTailwindConfig() | ||
} catch (err) { | ||
console.error(err); | ||
} |