Skip to content

Commit

Permalink
Convert resolveTailwindConfig.js to ESM (#108)
Browse files Browse the repository at this point in the history
* Convert resolve script to ESM

* Fix missing config default key

* Clean up resolveTailwindConfig.js

* Remove async statement on parseConfig fn
ptrckvzn authored Feb 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a74d4dd commit 8982118
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/resolveTailwindConfig.js
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);
}

0 comments on commit 8982118

Please sign in to comment.