Skip to content
Merged
Prev Previous commit
Next Next commit
migrate passed in files (or discover files)
  • Loading branch information
RobinMalfait committed Sep 18, 2024
commit 741e5ac35d8b2c0000e817cebd9d9ad05f11cb6d
4 changes: 3 additions & 1 deletion packages/@tailwindcss-upgrade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
},
"dependencies": {
"enhanced-resolve": "^5.17.1",
"fast-glob": "^3.3.2",
"mri": "^1.2.0",
"picocolors": "^1.0.1",
"postcss": "^8.4.41",
"postcss-import": "^16.1.0"
"postcss-import": "^16.1.0",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"@types/node": "catalog:",
Expand Down
47 changes: 43 additions & 4 deletions packages/@tailwindcss-upgrade/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import fastGlob from 'fast-glob'
import { execSync } from 'node:child_process'
import path from 'node:path'
import pc from 'picocolors'
Expand All @@ -23,8 +24,6 @@ if (flags['--help']) {
process.exit(0)
}

const file = flags._[0]

async function run() {
eprintln(header())
eprintln()
Expand All @@ -45,9 +44,49 @@ async function run() {
}
}

await migrate(path.resolve(process.cwd(), file))
// Use provided files
let files = flags._.map((file) => path.resolve(process.cwd(), file))

// Discover CSS files in case no files were provided
if (files.length === 0) {
wordWrap(
'No files provided. Searching for CSS files in the current directory and its subdirectories…',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.blue('\u2502')} ${line}`))
eprintln()

files = await fastGlob(['**/*.css'], {
absolute: true,
ignore: ['**/node_modules', '**/vendor'],
})
}

// Ensure we are only dealing with CSS files
files = files.filter((file) => file.endsWith('.css'))

// Migrate each file
await Promise.allSettled(files.map((file) => migrate(file)))

// Figure out if we made any changes
let stdout = execSync('git status --porcelain', { encoding: 'utf-8' })
if (stdout.trim()) {
wordWrap(
'Migration complete. Verify the changes and commit them to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
} else {
wordWrap(
'Migration complete. No changes were made to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
}
}

run()
.then(() => process.exit(0))
.catch(() => process.exit(1))
.catch((err) => {
console.error(err)
process.exit(1)
})
18 changes: 0 additions & 18 deletions packages/@tailwindcss-upgrade/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import pc from 'picocolors'
import postcss from 'postcss'
import { migrateAtApply } from './codemods/migrate-at-apply'
import { eprintln, wordWrap } from './utils/renderer'

export async function migrateContents(contents: string, file?: string) {
return postcss()
Expand All @@ -18,19 +15,4 @@ export async function migrate(file: string) {
let contents = await fs.readFile(fullPath, 'utf-8')

await fs.writeFile(fullPath, await migrateContents(contents, fullPath))

let stdout = execSync('git status --porcelain', { encoding: 'utf-8' })
if (stdout.trim()) {
wordWrap(
'Migration complete. Verify the changes and commit them to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
} else {
wordWrap(
'Migration complete. No changes were made to your repository.',
process.stderr.columns - 5 - 4,
).map((line) => eprintln(`${pc.green('\u2502')} ${line}`))
eprintln()
}
}
2 changes: 1 addition & 1 deletion packages/@tailwindcss-upgrade/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineConfig} from 'tsup'
import { defineConfig } from 'tsup'

export default defineConfig({
format: ['esm'],
Expand Down