|
8 | 8 | // |
9 | 9 |
|
10 | 10 | import * as child_process from 'child_process' |
| 11 | +import * as fs from 'fs-extra' |
11 | 12 | import * as path from 'path' |
12 | | -import { fs } from '../packages/core/src/shared' |
13 | | -import { readdirSync } from 'fs' |
14 | 13 |
|
15 | | -async function main() { |
16 | | - // Must run from a subproject root folder, e.g packages/toolkit |
17 | | - const cwd = process.cwd() |
18 | | - const fileContents = await fs.readFileText('./package.json') |
19 | | - const packageJson = JSON.parse(fileContents) |
20 | | - const changesDirectory = path.join(cwd, '.changes') |
21 | | - const nextReleaseDirectory = path.join(changesDirectory, 'next-release') |
22 | | - const changesFile = path.join(changesDirectory, `${packageJson.version}.json`) |
| 14 | +// Must run from a subproject root folder, e.g packages/toolkit |
| 15 | +const cwd = process.cwd() |
| 16 | +const packageJson = JSON.parse(fs.readFileSync('./package.json', { encoding: 'utf-8' })) |
| 17 | +const changesDirectory = path.join(cwd, '.changes') |
| 18 | +const nextReleaseDirectory = path.join(changesDirectory, 'next-release') |
| 19 | +const changesFile = path.join(changesDirectory, `${packageJson.version}.json`) |
23 | 20 |
|
24 | | - await fs.mkdir(nextReleaseDirectory) |
| 21 | +fs.mkdirpSync(nextReleaseDirectory) |
25 | 22 |
|
26 | | - const changeFiles = readdirSync(nextReleaseDirectory) |
27 | | - if (changeFiles.length === 0) { |
28 | | - console.warn('no changes to release (missing .changes/ directory)') |
29 | | - process.exit() |
30 | | - } |
31 | | - if (await fs.exists(changesFile)) { |
32 | | - console.log(`error: changelog data file already exists: ${changesFile}`) |
33 | | - process.exit(-1) |
34 | | - } |
35 | | - |
36 | | - const timestamp = new Date().toISOString().split('T')[0] |
37 | | - const changelog: any = { |
38 | | - date: timestamp, |
39 | | - version: packageJson.version, |
40 | | - entries: [], |
41 | | - } |
| 23 | +const changeFiles = fs.readdirSync(nextReleaseDirectory) |
| 24 | +if (changeFiles.length === 0) { |
| 25 | + console.warn('no changes to release (missing .changes/ directory)') |
| 26 | + process.exit() |
| 27 | +} |
| 28 | +try { |
| 29 | + fs.accessSync(changesFile) |
| 30 | + console.log(`error: changelog data file already exists: ${changesFile}`) |
| 31 | + process.exit(-1) |
| 32 | +} catch (err) { |
| 33 | + // This is what we want to happen, the file should not exist |
| 34 | +} |
42 | 35 |
|
43 | | - for (const changeFile of changeFiles) { |
44 | | - const file = JSON.parse(fs.readFileBytes(path.join(nextReleaseDirectory, changeFile)).toString()) |
45 | | - changelog.entries.push(file) |
46 | | - } |
| 36 | +const timestamp = new Date().toISOString().split('T')[0] |
| 37 | +const changelog: any = { |
| 38 | + date: timestamp, |
| 39 | + version: packageJson.version, |
| 40 | + entries: [], |
| 41 | +} |
47 | 42 |
|
48 | | - changelog.entries.sort((x: { type: string }, y: { type: string }) => x.type.localeCompare(y.type)) |
| 43 | +for (const changeFile of changeFiles) { |
| 44 | + const file = JSON.parse(fs.readFileSync(path.join(nextReleaseDirectory, changeFile)).toString()) |
| 45 | + changelog.entries.push(file) |
| 46 | +} |
49 | 47 |
|
50 | | - // Write changelog file |
51 | | - await fs.writeFile(changesFile, JSON.stringify(changelog, undefined, '\t')) |
52 | | - const fileData = fs.readFileBytes(path.join(cwd, 'CHANGELOG.md')) |
53 | | - let append = `## ${packageJson.version} ${timestamp}\n\n` |
54 | | - for (const file of changelog.entries) { |
55 | | - append += `- **${file.type}** ${file.description}\n` |
56 | | - } |
| 48 | +changelog.entries.sort((x: { type: string }, y: { type: string }) => x.type.localeCompare(y.type)) |
57 | 49 |
|
58 | | - append += '\n' + fileData.toString() |
59 | | - await fs.writeFile('CHANGELOG.md', append) |
| 50 | +// Write changelog file |
| 51 | +fs.writeFileSync(changesFile, JSON.stringify(changelog, undefined, '\t')) |
| 52 | +const fileData = fs.readFileSync(path.join(cwd, 'CHANGELOG.md')) |
| 53 | +let append = `## ${packageJson.version} ${timestamp}\n\n` |
| 54 | +for (const file of changelog.entries) { |
| 55 | + append += `- **${file.type}** ${file.description}\n` |
| 56 | +} |
60 | 57 |
|
61 | | - child_process.execSync(`git add ${changesDirectory}`) |
62 | | - child_process.execSync(`git rm -rf ${nextReleaseDirectory}`) |
63 | | - child_process.execSync('git add CHANGELOG.md') |
| 58 | +append += '\n' + fileData.toString() |
| 59 | +fs.writeFileSync('CHANGELOG.md', append) |
64 | 60 |
|
65 | | - console.log(changesFile) |
66 | | -} |
| 61 | +child_process.execSync(`git add ${changesDirectory}`) |
| 62 | +child_process.execSync(`git rm -rf ${nextReleaseDirectory}`) |
| 63 | +child_process.execSync('git add CHANGELOG.md') |
67 | 64 |
|
68 | | -void main() |
| 65 | +console.log(changesFile) |
0 commit comments