Skip to content

Commit be0f0a5

Browse files
committed
Add export and serve commands
1 parent 5d6e19d commit be0f0a5

File tree

10 files changed

+402
-79
lines changed

10 files changed

+402
-79
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.DS_Store
22
node_modules
33
/dist
4+
# static export build
5+
tcv-build
46

57
# local env files
68
.env.local

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,48 @@ Tailwind Config Viewer is a local UI tool for visualizing your Tailwind CSS conf
1010
Run `npx tailwind-config-viewer` from within the directory that contains your Tailwind configuration file.
1111

1212
### Globally
13-
`npm i tailwind-config-viewer`
13+
`npm i tailwind-config-viewer -g`
1414

1515
### Locally
1616
`npm i tailwind-config-viewer -D`
1717

18+
When installing locally, you can add an entry into the package.json scripts field to run and open the viewer:
19+
20+
```json
21+
"scripts": {
22+
"tailwind-config-viewer": "tailwind-config-viewer -o"
23+
}
24+
```
25+
1826
## Usage
27+
1928
Run the `tailwind-config-viewer` command from within the directory that contains your Tailwind configuration file.
2029

21-
## Options
22-
There are a few options you can provide to the `tailwind-config-viewer` command.
30+
## Commands
31+
32+
### serve (default)
33+
34+
The `serve` command is the default command. Running `tailwind-config-viewer` is the same as `tailwind-config-viewer serve`.
35+
36+
**Options**
2337

2438
|Option|Default|Description|
2539
|----|----|----|
2640
|-p, --port|`3000`|The port to run the viewer on. If occupied it will use next available port.|
2741
|-o, --open|`false`|Open the viewer in default browser|
2842
|-c, --config|`tailwind.config.js`|Path to your Tailwind config file|
2943

44+
### export
45+
46+
Exports the viewer to a directory that can be uploaded to a static host. It accepts the output directory path as its sole argument.
47+
48+
`tailwind-config-viewer export ./output-dir`
49+
50+
If an output directory isn't specificied it will be output to `tcv-build`.
51+
3052
## Roadmap
53+
54+
- [x] Add static export
3155
- [ ] Add support for loading custom fonts / font family section
3256
- [ ] Add Transition section
3357
- [ ] Dark Mode toggle

cli/export.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs-extra')
2+
const path = require('path')
3+
const crypto = require('crypto')
4+
const replace = require('replace-in-file')
5+
const { resolveConfigToJson } = require('../lib/tailwindConfigUtils')
6+
7+
module.exports = function (outputDir, config) {
8+
outputDir = path.resolve(process.cwd(), outputDir)
9+
10+
fs.removeSync(outputDir)
11+
fs.mkdirSync(outputDir)
12+
13+
const configJson = resolveConfigToJson(config)
14+
const configFileName = generateConfigFileNameFromJson(configJson)
15+
16+
fs.writeFileSync(path.resolve(outputDir, configFileName), configJson)
17+
18+
fs.copySync('./dist', outputDir)
19+
20+
replace.sync({
21+
files: `${outputDir}/index.html`,
22+
from: 'config.json',
23+
to: configFileName
24+
})
25+
}
26+
27+
function generateConfigFileNameFromJson (configJson) {
28+
const configFileHash = crypto.createHash('md5').update(configJson).digest('hex')
29+
30+
return `config.${configFileHash.substr(0, 8)}.json`
31+
}

cli/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
#!/usr/bin/env node
2+
const program = require('commander')
23

3-
require('../server/index.js')
4+
program
5+
.option('-c, --config <path>', 'Path to your Tailwind config file', 'tailwind.config.js')
6+
7+
program
8+
.command('serve', { isDefault: true })
9+
.description('Serve the viewer')
10+
.option('-p, --port <port>', 'Port to run the viewer on', 3000)
11+
.option('-o, --open', 'Open the viewer in default browser')
12+
.action(args => {
13+
require('../server')({
14+
port: args.port,
15+
tailwindConfigPath: program.config,
16+
shouldOpen: args.open
17+
})
18+
})
19+
20+
program
21+
.command('export [outputDir]')
22+
.description('Create a static export of the viewer')
23+
.action((outputDir = './tcv-build') => {
24+
require('./export')(outputDir, program.config)
25+
})
26+
27+
program.parse(process.argv)

lib/tailwindConfigUtils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('path')
2+
const resolveTailwindConfig = require('tailwindcss/resolveConfig')
3+
4+
const resolveConfigPath = configPath => path.resolve(process.cwd(), configPath)
5+
6+
const resolveConfig = configPath => {
7+
const config = require(resolveConfigPath(configPath))
8+
return resolveTailwindConfig(config)
9+
}
10+
11+
const resolveConfigToJson = configPath => JSON.stringify(resolveConfig(configPath))
12+
13+
module.exports = {
14+
resolveConfig,
15+
resolveConfigPath,
16+
resolveConfigToJson
17+
}

0 commit comments

Comments
 (0)