Comparing version 0.0.4 to 0.0.5
@@ -0,0 +0,0 @@ #!/usr/bin/env node |
244
index.js
#!/usr/bin/env node | ||
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs' | ||
import { join, extname, basename, resolve } from 'path' | ||
import { createServer } from 'http' | ||
import { readFile } from 'fs/promises' | ||
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync, watch } from 'fs' | ||
import { join, extname, basename } from 'path' | ||
import { transpileCSSX } from './src/core.js' | ||
import { Command } from 'commander' | ||
import { transpileCSSX } from './core.js' | ||
const packageJSON = JSON.parse(readFileSync('./package.json')) | ||
const { version } = packageJSON | ||
const args = process.argv.slice(2) | ||
args.forEach((arg) => { | ||
if (arg === '--help') { | ||
console.log(`CSSX v${version}`) | ||
console.log('Usage: node index.js [options]') | ||
console.log('Options:') | ||
console.log(' --output=dist Output directory') | ||
console.log(' --help Show help') | ||
console.log(' --version Show version number') | ||
console.log('Examples:') | ||
console.log(' node index.js --output=dist') | ||
process.exit(0) | ||
const program = new Command() | ||
program | ||
.version(version) | ||
.option('-h, --help', 'Show help') | ||
.option('-o, --output <dir>', 'Specify output directory', 'dist') | ||
.option('--serve', 'Start a local server') | ||
.option('--watch', 'Watch for file changes and rebuild automatically') | ||
.parse(process.argv) | ||
const options = program.opts() | ||
const srcDir = join(process.cwd(), 'src') | ||
const pagesDir = join(process.cwd(), 'src', 'pages') | ||
const publicDir = join(process.cwd(), 'public') | ||
const buildDir = join(process.cwd(), options.output) | ||
if (options.help) { | ||
console.log(`CSSX v${version}`) | ||
console.log('Usage: cssx-build [options]') | ||
console.log('Options:') | ||
console.log(' -o, --output=dist Output directory') | ||
console.log(' --serve Start a local server') | ||
console.log(' --watch Watch for file changes and rebuild automatically') | ||
console.log(' --help Show help') | ||
console.log(' --version Show version number') | ||
console.log('Examples:') | ||
console.log(' cssx-build --output=dist') | ||
process.exit(0) | ||
} | ||
function setupBuildDirectory (buildDir) { | ||
if (existsSync(buildDir)) { | ||
readdirSync(buildDir).forEach((file) => unlinkSync(join(buildDir, file))) | ||
} else { | ||
mkdirSync(buildDir) | ||
} | ||
if (arg === '--version') { | ||
console.log(version) | ||
process.exit(0) | ||
} | ||
function copyPublicDirectory (buildDir) { | ||
if (existsSync(publicDir)) { | ||
readdirSync(publicDir).forEach((file) => { | ||
const inputFile = join(publicDir, file) | ||
const outputFile = join(buildDir, file) | ||
writeFileSync(outputFile, readFileSync(inputFile)) | ||
}) | ||
} else { | ||
console.log('No public directory found') | ||
} | ||
}) | ||
} | ||
let buildDir = 'dist' | ||
args | ||
.filter((arg) => arg.startsWith('--output')) | ||
.forEach((arg) => { | ||
buildDir = arg.split('=')[1] || 'dist' | ||
}) | ||
function processCSSXFiles (buildDir) { | ||
if (existsSync(pagesDir)) { | ||
readdirSync(pagesDir).forEach((file) => { | ||
if (extname(file) === '.cssx') { | ||
const inputFile = join(pagesDir, file) | ||
const content = readFileSync(inputFile, 'utf8') | ||
const htmlContent = transpileCSSX(content) | ||
const cwd = process.cwd() | ||
if (existsSync(join(cwd, buildDir))) { | ||
readdirSync(join(cwd, buildDir)).forEach((file) => { | ||
unlinkSync(join(cwd, buildDir, file)) | ||
}) | ||
} else { | ||
mkdirSync(join(cwd, buildDir)) | ||
const name = basename(file, '.cssx') | ||
const outputFile = join(buildDir, `${name}.html`) | ||
writeFileSync(outputFile, htmlContent, 'utf8') | ||
console.log(`Generated ${name}.html with CSSX`) | ||
} | ||
}) | ||
} else { | ||
console.error(`${pagesDir} directory not found`) | ||
} | ||
} | ||
try { | ||
const publicDirectory = join(cwd, 'public') | ||
readdirSync(publicDirectory).forEach((file) => { | ||
const inputFile = join(publicDirectory, file) | ||
const outputFile = join(cwd, buildDir, file) | ||
writeFileSync(outputFile, readFileSync(inputFile)) | ||
setupBuildDirectory(buildDir) | ||
copyPublicDirectory(buildDir) | ||
processCSSXFiles(buildDir) | ||
console.log(`Build completed in ${buildDir} directory`) | ||
if (options.serve && options.watch) { | ||
import('servor').then(async (servorModule) => { | ||
const servor = servorModule.default | ||
try { | ||
const instance = await servor({ | ||
root: 'dist', | ||
static: true, | ||
reload: true, | ||
port: 3000 | ||
}) | ||
console.log('\nš Dev server running at http://localhost:3000') | ||
console.log('š” Live reload enabled') | ||
if (options.watch) { | ||
console.log('\nš Watching for changes...') | ||
console.log(`š Directory: ${pagesDir}`) | ||
let isProcessing = false | ||
const rebuild = async (filename) => { | ||
if (isProcessing) return | ||
try { | ||
isProcessing = true | ||
console.log(`\nš Rebuilding due to changes in ${filename}`) | ||
setupBuildDirectory(buildDir) | ||
copyPublicDirectory(buildDir) | ||
processCSSXFiles(buildDir) | ||
console.log('āØ Build completed') | ||
} catch (error) { | ||
console.error('ā Build failed:', error) | ||
} finally { | ||
isProcessing = false | ||
} | ||
} | ||
const watcher = watch(srcDir, { recursive: true }, (eventType, filename) => { | ||
if (filename && filename.endsWith('.cssx')) { | ||
console.log(`\nš ${eventType === 'change' ? 'Modified' : 'Changed'}: ${filename}`) | ||
rebuild(filename) | ||
} | ||
}) | ||
const cleanup = () => { | ||
console.log('\nš Stopping server and watch mode...') | ||
watcher.close() | ||
instance.close() | ||
process.exit(0) | ||
} | ||
process.on('SIGINT', cleanup) | ||
process.on('SIGTERM', cleanup) | ||
console.log('\nš Doing initial build...') | ||
rebuild('initial build') | ||
} | ||
} catch (err) { | ||
console.error('ā Failed to start server:', err) | ||
process.exit(1) | ||
} | ||
}).catch(err => { | ||
console.error('ā Failed to import servor:', err) | ||
process.exit(1) | ||
}) | ||
} catch (err) { | ||
console.log('No public directory found') | ||
} | ||
const pagesDirectory = join(cwd, 'src/pages') | ||
readdirSync(pagesDirectory).forEach((file) => { | ||
if (extname(file) === '.cssx') { | ||
const inputFile = join(pagesDirectory, file) | ||
const content = readFileSync(inputFile, 'utf8') | ||
if (options.watch) { | ||
console.log('\nš Watching for changes...') | ||
console.log(`š Directory: ${pagesDir}`) | ||
const htmlContent = transpileCSSX(content) | ||
let isProcessing = false | ||
const name = basename(file, '.cssx') | ||
const outputFile = join(cwd, buildDir, `${name}.html`) | ||
writeFileSync(outputFile, htmlContent, 'utf8') | ||
const rebuild = async (filename) => { | ||
if (isProcessing) return | ||
console.log(`Generated ${name}.html with CSSX`) | ||
try { | ||
isProcessing = true | ||
console.log(`\nš Rebuilding due to changes in ${filename}`) | ||
setupBuildDirectory(buildDir) | ||
copyPublicDirectory(buildDir) | ||
processCSSXFiles(buildDir) | ||
console.log('āØ Build completed') | ||
} catch (error) { | ||
console.error('ā Build failed:', error) | ||
} finally { | ||
isProcessing = false | ||
} | ||
} | ||
}) | ||
console.log(`Build completed in /${buildDir} directory`) | ||
const server = createServer(async (req, res) => { | ||
const url = req.url === '/' ? '/index.html' : req.url | ||
const path = resolve(cwd, buildDir, `.${url}`) | ||
const watcher = watch(srcDir, { recursive: true }, (eventType, filename) => { | ||
if (filename && filename.endsWith('.cssx')) { | ||
console.log(`\nš ${eventType === 'change' ? 'Modified' : 'Changed'}: ${filename}`) | ||
rebuild(filename) | ||
} | ||
}) | ||
try { | ||
const data = await readFile(path) | ||
res.end(data) | ||
} catch (err) { | ||
res.statusCode = 404 | ||
res.end('Not Found') | ||
const cleanup = () => { | ||
console.log('\nš Stopping server and watch mode...') | ||
watcher.close() | ||
process.exit(0) | ||
} | ||
}) | ||
if (args.includes('--serve')) { | ||
server.listen(3000, () => { | ||
console.log('Server running at http://localhost:3000/') | ||
}) | ||
process.on('SIGINT', cleanup) | ||
process.on('SIGTERM', cleanup) | ||
console.log('\nš Doing initial build...') | ||
rebuild('initial build') | ||
} |
{ | ||
"name": "cssx-build", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Compile CSS as a programming language", | ||
"author": "salteadorneo", | ||
"license": "MIT", | ||
"homepage": "https://salteadorneo.github.io/cssx/", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/salteadorneo/cssx.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/salteadorneo/cssx/issues", | ||
"email": "hola@salteadorneo.dev" | ||
}, | ||
"keywords": [ | ||
@@ -8,0 +17,0 @@ "cssx", |
@@ -5,10 +5,7 @@ # CSSX | ||
> [!WARNING] | ||
> | ||
> This project is still in development and not ready for production use. | ||
## Documentation | ||
## Requirements | ||
- [Getting Started](https://cssx.js.org/docs.html) - Learn how to get started with CSSX. | ||
- [Reference](https://cssx.js.org/reference.html) - Learn about the CSSX syntax and features. | ||
- [Node.js 20+](https://nodejs.org/en/) | ||
## Usage | ||
@@ -38,3 +35,2 @@ | ||
This will compile your CSSX files to CSS in the `dist` directory. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
17945
456
0
1
3
0
35