Skip to content

Commit

Permalink
improve compat mode
Browse files Browse the repository at this point in the history
We will use a glob so that we can move all *.postcss7.* files to just
*.* likewise we will also backup to *.* to *.postcss8.* for restoring
purposes.

Concrete example:

- Current state:
  - index.js            // PostCSS 8 implementation
  - index.postcss7.js   // PostCSS 7 implementation

- Run "compat"
  - index.js            // PostCSS 7 implementation
  - index.postcss7.js   // PostCSS 7 implementation
  - index.postcss8.js   // PostCSS 8 implementation (Backup of original)

- Run "compat:restore"
  - index.js            // PostCSS 8 implementation
  - index.postcss7.js   // PostCSS 7 implementation
  - X index.postcss8.js // PostCSS 8 implementation (Removed)
  • Loading branch information
RobinMalfait committed Jun 17, 2021
1 parent 4fe270c commit 93a8dd9
Showing 1 changed file with 46 additions and 42 deletions.
88 changes: 46 additions & 42 deletions scripts/compat.js
@@ -1,81 +1,85 @@
const fs = require('fs')
const path = require('path')
const merge = require('lodash/merge')
let fs = require('fs')
let path = require('path')
let merge = require('lodash/merge')
let fastGlob = require('fast-glob')

function fromRootPath(...paths) {
return path.resolve(process.cwd(), ...paths)
}

function copy(fromPath, toPath) {
fs.copyFileSync(fromPath, toPath)
}
let postcss7 = fastGlob.sync(['./**/*.postcss7.*']).filter((file) => !file.startsWith('lib/'))
let postcss8 = fastGlob.sync(['./**/*.postcss8.*']).filter((file) => !file.startsWith('lib/'))

if (process.argv.includes('--prepare')) {
if (
fs.existsSync(fromRootPath('package.postcss8.json')) ||
fs.existsSync(fromRootPath('src', 'index.postcss8.js'))
) {
if (postcss8.length > 0) {
console.error('\n\n[ABORT] Already in PostCSS 7 compatibility mode!\n\n')
process.exit(1)
}

const mainPackageJson = require('../package.json')
const compatPackageJson = require('../package.postcss7.json')
let mainPackageJson = require('../package.json')
let compatPackageJson = require('../package.postcss7.json')

// 1. Backup original package.json file
copy(fromRootPath('package.json'), fromRootPath('package.postcss8.json'))
// Use postcss7 files
for (let file of postcss7) {
let bareFile = file.replace('.postcss7', '')
let postcss8File = file.replace('.postcss7', '.postcss8')

// 2. Backup src/index.js file
copy(fromRootPath('src', 'index.js'), fromRootPath('src', 'index.postcss8.js'))
// Backup
copy(fromRootPath(bareFile), fromRootPath(postcss8File))

// 3. Use the PostCSS 7 compat file
copy(fromRootPath('src', 'index.postcss7.js'), fromRootPath('src', 'index.js'))
// Swap
copy(fromRootPath(file), fromRootPath(bareFile))
}

// 4. Deep merge package.json contents
const packageJson = merge({}, mainPackageJson, compatPackageJson)
// Deep merge package.json contents
let packageJson = merge({}, mainPackageJson, compatPackageJson)

// 5. Remove peerDependencies
// Remove peerDependencies
delete packageJson.peerDependencies

// 6. Cleanup devDependencies
// Cleanup devDependencies
for (let key in packageJson.devDependencies) {
if (key.includes('postcss')) delete packageJson.devDependencies[key]
}

// 7. Use new name
// Use new name
packageJson.name = '@tailwindcss/postcss7-compat'

// 8. Make sure you can publish
// Make sure you can publish
packageJson.publishConfig = { access: 'public' }

// 9. Write package.json with the new contents
// Write package.json with the new contents
fs.writeFileSync(fromRootPath('package.json'), JSON.stringify(packageJson, null, 2), 'utf8')

// 10. Print some useful information to make publishing easy
// Print some useful information to make publishing easy
console.log()
console.log('You can safely publish `tailwindcss` in PostCSS 7 compatibility mode:\n')
console.log()
} else if (process.argv.includes('--restore')) {
if (
!fs.existsSync(fromRootPath('package.postcss8.json')) ||
!fs.existsSync(fromRootPath('src', 'index.postcss8.js'))
) {
if (postcss8.length === 0) {
console.error('\n\n[ABORT] Already in latest PostCSS mode!\n\n')
process.exit(1)
}

// 1. Restore original package.json file
copy(fromRootPath('package.postcss8.json'), fromRootPath('package.json'))
// Use postcss8 files
for (let file of postcss8) {
let bareFile = file.replace('.postcss8', '')

// 2. Restore src/index.js file
copy(fromRootPath('src', 'index.postcss8.js'), fromRootPath('src', 'index.js'))
// Restore
copy(fromRootPath(file), fromRootPath(bareFile))

// 3. Cleanup PostCSS 8 related files
fs.unlinkSync(fromRootPath('package.postcss8.json'))
fs.unlinkSync(fromRootPath('src', 'index.postcss8.js'))
// Remove
fs.unlinkSync(fromRootPath(file))
}

// 4. Done
// Done
console.log()
console.log('Restored from PostCSS 7 mode to latest PostCSS mode!')
console.log()
}

// ---

function fromRootPath(...paths) {
return path.resolve(process.cwd(), ...paths)
}

function copy(fromPath, toPath) {
fs.copyFileSync(fromPath, toPath)
}

0 comments on commit 93a8dd9

Please sign in to comment.