Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"webpackbar": "^5.0.0"
},
"devDependencies": {
"@npmcli/arborist": "^9.1.1",
"@rc-component/father-plugin": "^2.1.2",
"@types/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0",
Expand All @@ -122,6 +123,7 @@
"eslint-plugin-react": "^7.13.0",
"father": "^4.5.2",
"jest": "^27.0.3",
"npm-packlist": "^10.0.0",
"pre-commit": "1.x"
},
"pre-commit": [
Expand Down
90 changes: 69 additions & 21 deletions src/lint/checkDiff.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getProjectPath } from '../utils/projectHelper';
import fs from 'fs';
import { join } from 'path';
import chalk from 'chalk';
import fetch from 'node-fetch';
import readline from 'readline';
import minimist from 'minimist';
import Arborist from '@npmcli/arborist';
import packlist from 'npm-packlist';

const argv = minimist(process.argv.slice(2));

Expand Down Expand Up @@ -41,27 +42,68 @@ export default function (
const mergedVersion = getMajorVersion(packageVersion, argv.version);
console.log(chalk.cyan(`Fetching latest version file list...${packageName}${mergedVersion}`));

fetch(`https://unpkg.com/${packageName}${mergedVersion}/?meta`)
.then(res => {
const version = getVersionFromURL(res.url, packageName);
return res.json().then((json: object) => ({ version, ...json }));
function getLatestVersionFileList() {
return fetch(`https://unpkg.com/${packageName}${mergedVersion}/?meta`)
.then(res => {
const version = getVersionFromURL(res.url, packageName);
return res.json().then((json: object) => ({ version, ...json }));
})
.then(({ version, files: pkgFiles }: { version: string; files: FileItem[] }) => {
function flattenPath(files: FileItem[], fileList: string[] = []): string[] {
(files || []).forEach(({ path, files: subFiles }) => {
const realPath = argv.path ? join(argv.path, path) : path
fileList.push(realPath);
flattenPath(subFiles, fileList);
});
return fileList;
}
return { version, fileList: flattenPath(pkgFiles) };
})
}

function getLocalVersionFileList() {
const arborist = new Arborist({ path: getProjectPath() });
return arborist.loadActual().then(packlist) as Promise<string[]>;
}


Promise.all([
getLocalVersionFileList(),
getLatestVersionFileList(),
])
.then(([localFiles, { version, fileList }]) => {
const localSet = new Set(localFiles);
const remoteSet = new Set(fileList);

const missingFiles: string[] = [];
const addedFiles: string[] = [];

const allFiles = new Set([...fileList, ...localFiles]);
allFiles.forEach(filePath => {
if (!localSet.has(filePath)) {
missingFiles.push(filePath);
} else if (!remoteSet.has(filePath)) {
addedFiles.push(filePath);
}
});
return { missingFiles, addedFiles, version };
})
.then(({ version, files: pkgFiles }: { version: string; files: FileItem[] }) => {
function flattenPath(files: FileItem[], fileList: string[] = []): string[] {
(files || []).forEach(({ path, files: subFiles }) => {
fileList.push(path);
flattenPath(subFiles, fileList);
.then(({ missingFiles, addedFiles, version }) => {


if (addedFiles.length) {
console.log(
chalk.yellow(`⚠️ Some file added in current build (last version: ${version}):`)
);
addedFiles.forEach(filePath => {
console.log(` + ${filePath}`);
});
return fileList;
}
return { version, fileList: flattenPath(pkgFiles) };
})
.then(({ version, fileList }) => {
const missingFiles = fileList.filter(filePath => {
const concatFilePath = argv.path ? join(argv.path, filePath) : filePath;

return !fs.existsSync(getProjectPath(concatFilePath));
});
// Separator
console.log();
console.log(chalk.gray(`-`.repeat(process.stdout.columns || 64)));
console.log();
}

if (missingFiles.length) {
console.log(
Expand All @@ -70,12 +112,18 @@ export default function (
missingFiles.forEach(filePath => {
console.log(` - ${filePath}`);
});
return Promise.reject('Please double confirm with files.');
}

const total = missingFiles.length + addedFiles.length;

if (total) {
return Promise.reject(
new Error(`Please double confirm with files. ${missingFiles.length} missing, ${addedFiles.length} added.`)
);
}
console.log(
chalk.green('✅ Nothing missing compare to latest version:'),
chalk.yellow(version)
chalk.gray(version)
);
return 0;
})
Expand Down