|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const merge = require('lodash/merge') |
| 4 | + |
| 5 | +function fromRootPath(...paths) { |
| 6 | + return path.resolve(process.cwd(), ...paths) |
| 7 | +} |
| 8 | + |
| 9 | +function backupPath(...paths) { |
| 10 | + return path.resolve(process.cwd(), 'node_modules', '__tw_cache__', ...paths) |
| 11 | +} |
| 12 | + |
| 13 | +function copy(fromPath, toPath) { |
| 14 | + fs.mkdirSync(path.dirname(toPath), { recursive: true }) |
| 15 | + fs.copyFileSync(fromPath, toPath) |
| 16 | +} |
| 17 | + |
| 18 | +if (process.argv.includes('--prepare')) { |
| 19 | + const mainPackageJson = require('../package.json') |
| 20 | + const compatPackageJson = require('../package.postcss7.json') |
| 21 | + |
| 22 | + // 1. Backup original package.json file |
| 23 | + copy(fromRootPath('package.json'), backupPath('package.json')) |
| 24 | + |
| 25 | + // 2. Backup lib/index.js file |
| 26 | + copy(fromRootPath('lib', 'index.js'), backupPath('lib', 'index.js')) |
| 27 | + |
| 28 | + // 3. Use the postcss7 compat file |
| 29 | + copy(fromRootPath('lib', 'index.postcss7.js'), fromRootPath('lib', 'index.js')) |
| 30 | + |
| 31 | + // 4. Deep merge package.json contents |
| 32 | + const packageJson = merge({}, mainPackageJson, compatPackageJson) |
| 33 | + |
| 34 | + // 5. Write package.json with the new contents |
| 35 | + fs.writeFileSync(fromRootPath('package.json'), JSON.stringify(packageJson, null, 2), 'utf8') |
| 36 | + |
| 37 | + // 6. Print some useful information to make publishing easy |
| 38 | + console.log() |
| 39 | + console.log('You can safely publish `tailwindcss` in PostCSS 7 compatibility mode:\n') |
| 40 | + console.log( |
| 41 | + ['npm version', 'npm publish --tag compat', 'npm run compat:restore'] |
| 42 | + .map((v) => ` ${v}`) |
| 43 | + .join('\n') |
| 44 | + ) |
| 45 | + console.log() |
| 46 | +} else if (process.argv.includes('--restore')) { |
| 47 | + // 1. Restore original package.json file |
| 48 | + copy(backupPath('package.json'), fromRootPath('package.json')) |
| 49 | + fs.unlinkSync(backupPath('package.json')) |
| 50 | + |
| 51 | + // 2. Restore lib/index.js file |
| 52 | + copy(backupPath('lib', 'index.js'), fromRootPath('lib', 'index.js')) |
| 53 | + fs.unlinkSync(backupPath('lib', 'index.js')) |
| 54 | + |
| 55 | + // 3. Done |
| 56 | + console.log() |
| 57 | + console.log('Restored from PostCSS 7 mode to latest PostCSS mode!') |
| 58 | + console.log() |
| 59 | +} |
0 commit comments