-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flag to set registry url and print info
- Loading branch information
1 parent
ccd1bff
commit 8410b0b
Showing
16 changed files
with
442 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
Oops, something went wrong.