Skip to content

Commit

Permalink
Add flag to set registry url and print info
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotpirxie committed Aug 12, 2023
1 parent ccd1bff commit 8410b0b
Show file tree
Hide file tree
Showing 16 changed files with 442 additions and 323 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v10
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ npx dependency-time-machine --update --install --exclude-file exclude.txt
```

## Requirements
- Node.js >= 14
- Node.js >= 10

## How it works
Tool reads `package.json` file and finds all dependencies. Then it resolves all the versions from registry, sort them by date and
Expand Down Expand Up @@ -89,7 +89,9 @@ Options:
-c, --cache Cache resolved dependencies
-cf, --cache-file <file> Cache file (default: "./.dependency-time-machine/cache.json")
-e, --exclude <dependency> Exclude dependency from update, separated by comma
-x, --exclude-file <file> Exclude dependencies from file (default: "")
-x, --exclude-file <file> Exclude dependencies from file, one per line (default: "")
-r, --registry-url <url> Registry url (default: "https://registry.npmjs.org")
-pi, --print-info Print info about the packages
-h, --help display help for command
```

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts": {
"build": "tsc",
"test": "exit 0",
"dev": "npm run build && npm install -g . && dependency-time-machine",
"dev": "npm run build && npm install -g .",
"prettier": "prettier --write ."
},
"files": [
Expand Down
22 changes: 22 additions & 0 deletions src/exec/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from "fs";
import path from "path";

export function close({
auto,
cache,
cacheFile,
currentDir,
}: {
auto: boolean;
cache: boolean;
cacheFile: string;
currentDir: string;
}) {
if (auto && !cache) {
console.log("Cleaning up");
fs.unlinkSync(path.join(currentDir, cacheFile));
}

console.log("Done");
process.exit(0);
}
22 changes: 22 additions & 0 deletions src/exec/getDependenciesFromPackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LocalDependencies } from "../types/LocalDependencies";
import fs from "fs";

export function getDependenciesFromPackageJson({
packageFilePath,
}: {
packageFilePath: string;
}): LocalDependencies {
const packageJson = JSON.parse(fs.readFileSync(packageFilePath, "utf-8"));

const dependencies = packageJson.dependencies;
const devDependencies = packageJson.devDependencies;
const peerDependencies = packageJson.peerDependencies;
const optionalDependencies = packageJson.optionalDependencies;

return {
...dependencies,
...devDependencies,
...peerDependencies,
...optionalDependencies,
};
}
28 changes: 28 additions & 0 deletions src/exec/getExcludedDependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "fs";

export function getExcludedDependencies({
exclude,
excludeFilePath,
}: {
exclude: string;
excludeFilePath: string;
}): string[] {
const excludedDependencies: string[] = [];

if (
excludeFilePath &&
excludeFilePath.length > 0 &&
fs.existsSync(excludeFilePath)
) {
const excludeFileContent = fs.readFileSync(excludeFilePath, "utf-8");
const excludeFileDependencies = excludeFileContent.split("\n");
excludedDependencies.push(...excludeFileDependencies);
}

if (exclude && exclude.length > 0) {
const excludeDependencies = exclude.split(",");
excludedDependencies.push(...excludeDependencies);
}

return excludedDependencies;
}
70 changes: 70 additions & 0 deletions src/exec/getRemoteDependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { LocalDependencies } from "../types/LocalDependencies";
import { Dependency } from "../types/Dependency";
import fs from "fs";
import httpDependencyResolver from "../util/httpDependencyResolver";
import { isValidVersion } from "../util/semver";
import path from "path";

export async function getRemoteDependencies({
localDependencies,
cache,
cacheFilePath,
excludedDependencies,
registryUrl,
}: {
localDependencies: LocalDependencies;
cache: boolean;
cacheFilePath: string;
excludedDependencies: string[];
registryUrl: string;
}): Promise<Dependency[]> {
try {
const cacheExists = fs.existsSync(cacheFilePath);
if (cacheExists) {
return JSON.parse(fs.readFileSync(cacheFilePath, "utf-8"));
}

console.log("Fetching remote dependencies...");
let remoteDependencies: Dependency[] = [];
const dependenciesCount = Object.keys(localDependencies).length;
for (let i = 0; i < dependenciesCount; i++) {
const [name] = Object.entries(localDependencies)[i];
console.log(
`[${i + 1}/${dependenciesCount}] ${name} ${
excludedDependencies.includes(name) ? "(excluded)" : ""
}`
);
if (excludedDependencies.includes(name)) {
continue;
}
const newDependency = await httpDependencyResolver(name, registryUrl);
remoteDependencies.push(...newDependency);
}

remoteDependencies = remoteDependencies.filter((dependency) =>
isValidVersion(dependency.version)
);

const sortedRemoteDependencies = remoteDependencies
.sort((a, b) => {
return b.published.getTime() - a.published.getTime();
})
.reverse();

if (cache) {
fs.mkdirSync(path.dirname(cacheFilePath), {
recursive: true,
});

fs.writeFileSync(
cacheFilePath,
JSON.stringify(sortedRemoteDependencies, null, 2)
);
}

return sortedRemoteDependencies;
} catch (error) {
console.error(error);
process.exit(1);
}
}
13 changes: 13 additions & 0 deletions src/exec/installDependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import child_process from "child_process";

export function installDependency({
installScript,
currentDir,
}: {
installScript: string;
currentDir: string;
}) {
console.log("Installing new version...");
child_process.execSync(`cd ${currentDir} && ${installScript} && cd -`);
console.log("Installed");
}
53 changes: 53 additions & 0 deletions src/exec/printDependenciesInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { LocalDependencies } from "../types/LocalDependencies";
import { Dependency } from "../types/Dependency";
import { compareVersions } from "../util/semver";

export function printDependenciesInfo({
localDependencies,
sortedRemoteDependencies,
}: {
localDependencies: LocalDependencies;
sortedRemoteDependencies: Dependency[];
}) {
const remoteInfoAboutLocalDependencies = sortedRemoteDependencies
.filter((dependency) => {
return (
localDependencies[dependency.name] &&
compareVersions(
localDependencies[dependency.name],
dependency.version
) === 0
);
})
.sort((a, b) => {
return b.published.getTime() - a.published.getTime();
});

const averageReleaseTime =
remoteInfoAboutLocalDependencies.reduce((acc, dependency) => {
const date = new Date(dependency.published);
return acc + date.getTime();
}, 0) / remoteInfoAboutLocalDependencies.length;

const newestDependency = remoteInfoAboutLocalDependencies[0];

const oldestDependency =
remoteInfoAboutLocalDependencies[
remoteInfoAboutLocalDependencies.length - 1
];

const dependencyCount = Object.keys(localDependencies).length;

console.log(
JSON.stringify(
{
"Average release time": new Date(averageReleaseTime).toISOString(),
"Oldest dependency": oldestDependency,
"Newest dependency": newestDependency,
"Dependency count": dependencyCount,
},
null,
2
)
);
}
13 changes: 13 additions & 0 deletions src/exec/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import child_process from "child_process";

export function runTest({
testScript,
currentDir,
}: {
testScript: string;
currentDir: string;
}) {
console.log("Testing new version...");
child_process.execSync(`cd ${currentDir} && ${testScript} && cd -`);
console.log("Test passed");
}
32 changes: 32 additions & 0 deletions src/exec/updatePackageFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Dependency } from "../types/Dependency";
import fs from "fs";

export function updatePackageFile({
packageFilePath,
dependencyToUpdate,
}: {
dependencyToUpdate: Dependency;
packageFilePath: string;
}) {
console.log(
`Updating ${dependencyToUpdate.name}@${dependencyToUpdate.version} in ${packageFilePath}...`
);
const packageJson = JSON.parse(fs.readFileSync(packageFilePath, "utf-8"));
const { dependencies, devDependencies, peerDependencies } = packageJson;

if (dependencies && dependencies.hasOwnProperty(dependencyToUpdate.name)) {
dependencies[dependencyToUpdate.name] = dependencyToUpdate.version;
} else if (
devDependencies &&
devDependencies.hasOwnProperty(dependencyToUpdate.name)
) {
devDependencies[dependencyToUpdate.name] = dependencyToUpdate.version;
} else if (
peerDependencies &&
peerDependencies.hasOwnProperty(dependencyToUpdate.name)
) {
peerDependencies[dependencyToUpdate.name] = dependencyToUpdate.version;
}

fs.writeFileSync(packageFilePath, JSON.stringify(packageJson, null, 2));
}
Loading

0 comments on commit 8410b0b

Please sign in to comment.