fix(repo): fix flaky cross-os new line check (#5938)
This commit is contained in:
parent
a2c2d94050
commit
6ca30d86be
@ -270,6 +270,9 @@ describe('migrate', () => {
|
|||||||
// updates package.json
|
// updates package.json
|
||||||
const packageJson = readJson(`package.json`);
|
const packageJson = readJson(`package.json`);
|
||||||
expect(packageJson.dependencies['migrate-child-package']).toEqual('9.0.0');
|
expect(packageJson.dependencies['migrate-child-package']).toEqual('9.0.0');
|
||||||
|
// should keep new line on package
|
||||||
|
const packageContent = readFile('package.json');
|
||||||
|
expect(packageContent.charCodeAt(packageContent.length - 1)).toEqual(10);
|
||||||
|
|
||||||
// creates migrations.json
|
// creates migrations.json
|
||||||
const migrationsJson = readJson(`migrations.json`);
|
const migrationsJson = readJson(`migrations.json`);
|
||||||
|
|||||||
@ -472,7 +472,7 @@ export function readJson(f: string): any {
|
|||||||
|
|
||||||
export function readFile(f: string) {
|
export function readFile(f: string) {
|
||||||
const ff = f.startsWith('/') ? f : tmpProjPath(f);
|
const ff = f.startsWith('/') ? f : tmpProjPath(f);
|
||||||
return readFileSync(ff).toString();
|
return readFileSync(ff, 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function rmDist() {
|
export function rmDist() {
|
||||||
|
|||||||
@ -13,7 +13,11 @@ import {
|
|||||||
} from '../shared/package-manager';
|
} from '../shared/package-manager';
|
||||||
import { FsTree } from '../shared/tree';
|
import { FsTree } from '../shared/tree';
|
||||||
import { flushChanges } from './generate';
|
import { flushChanges } from './generate';
|
||||||
import { readJsonFile, writeJsonFile } from '../utils/fileutils';
|
import {
|
||||||
|
JsonReadOptions,
|
||||||
|
readJsonFile,
|
||||||
|
writeJsonFile,
|
||||||
|
} from '../utils/fileutils';
|
||||||
|
|
||||||
export type MigrationsJson = {
|
export type MigrationsJson = {
|
||||||
version: string;
|
version: string;
|
||||||
@ -503,7 +507,8 @@ function updatePackageJson(
|
|||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const packageJsonPath = join(root, 'package.json');
|
const packageJsonPath = join(root, 'package.json');
|
||||||
const json = readJsonFile(packageJsonPath);
|
const parseOptions: JsonReadOptions = {};
|
||||||
|
const json = readJsonFile(packageJsonPath, parseOptions);
|
||||||
Object.keys(updatedPackages).forEach((p) => {
|
Object.keys(updatedPackages).forEach((p) => {
|
||||||
if (json.devDependencies && json.devDependencies[p]) {
|
if (json.devDependencies && json.devDependencies[p]) {
|
||||||
json.devDependencies[p] = updatedPackages[p].version;
|
json.devDependencies[p] = updatedPackages[p].version;
|
||||||
@ -514,7 +519,9 @@ function updatePackageJson(
|
|||||||
json.dependencies[p] = updatedPackages[p].version;
|
json.dependencies[p] = updatedPackages[p].version;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
writeJsonFile(packageJsonPath, json);
|
writeJsonFile(packageJsonPath, json, {
|
||||||
|
appendNewLine: parseOptions.endsWithNewline,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateMigrationsJsonAndUpdatePackageJson(
|
async function generateMigrationsJsonAndUpdatePackageJson(
|
||||||
|
|||||||
@ -4,6 +4,22 @@ import { readFileSync, writeFileSync } from 'fs';
|
|||||||
import { dirname } from 'path';
|
import { dirname } from 'path';
|
||||||
import { ensureDirSync } from 'fs-extra';
|
import { ensureDirSync } from 'fs-extra';
|
||||||
|
|
||||||
|
export interface JsonReadOptions extends JsonParseOptions {
|
||||||
|
/**
|
||||||
|
* mutable field recording whether JSON ends with new line
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
endsWithNewline?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface JsonWriteOptions extends JsonSerializeOptions {
|
||||||
|
/**
|
||||||
|
* whether to append new line at the end of JSON file
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
appendNewLine?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a JSON file and returns the object the JSON content represents.
|
* Reads a JSON file and returns the object the JSON content represents.
|
||||||
*
|
*
|
||||||
@ -13,9 +29,13 @@ import { ensureDirSync } from 'fs-extra';
|
|||||||
*/
|
*/
|
||||||
export function readJsonFile<T extends object = any>(
|
export function readJsonFile<T extends object = any>(
|
||||||
path: string,
|
path: string,
|
||||||
options?: JsonParseOptions
|
options?: JsonReadOptions
|
||||||
): T {
|
): T {
|
||||||
return parseJson<T>(readFileSync(path, 'utf-8'), options);
|
const content = readFileSync(path, 'utf-8');
|
||||||
|
if (options) {
|
||||||
|
options.endsWithNewline = content.charCodeAt(content.length - 1) === 10;
|
||||||
|
}
|
||||||
|
return parseJson<T>(content, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,8 +48,12 @@ export function readJsonFile<T extends object = any>(
|
|||||||
export function writeJsonFile<T extends object = object>(
|
export function writeJsonFile<T extends object = object>(
|
||||||
path: string,
|
path: string,
|
||||||
data: T,
|
data: T,
|
||||||
options?: JsonSerializeOptions
|
options?: JsonWriteOptions
|
||||||
): void {
|
): void {
|
||||||
ensureDirSync(dirname(path));
|
ensureDirSync(dirname(path));
|
||||||
writeFileSync(path, serializeJson(data, options), { encoding: 'utf-8' });
|
const serializedJson = serializeJson(data, options);
|
||||||
|
const content = options?.appendNewLine
|
||||||
|
? `${serializedJson}\n`
|
||||||
|
: serializedJson;
|
||||||
|
writeFileSync(path, content, { encoding: 'utf-8' });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user