-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf-to-darkroom.js
executable file
·104 lines (89 loc) · 3.18 KB
/
sf-to-darkroom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
const fs = require("fs")
const path = require("path")
const { execSync } = require("child_process")
function detectPackageManager() {
if (fs.existsSync("pnpm-lock.yaml")) {
return "pnpm"
} else if (fs.existsSync("yarn.lock")) {
return "yarn"
} else {
return "npm"
}
}
function getLatestPackageVersion(packageName) {
const command = `npm view ${packageName} version`
return execSync(command, { encoding: "utf8" }).trim()
}
function processFile(filePath) {
const fileContent = fs.readFileSync(filePath, "utf8")
const updatedContent = fileContent
.replace(/@studio-freight\/lenis/g, "lenis")
.replace(/@studio-freight\/react-lenis/g, "lenis/react")
.replace(/@studio-freight\//g, "@darkroom.engineering/")
fs.writeFileSync(filePath, updatedContent, "utf8")
}
function processDirectory(dirPath) {
const entries = fs.readdirSync(dirPath, { withFileTypes: true })
entries.forEach((entry) => {
const fullPath = path.join(dirPath, entry.name)
if (entry.isDirectory()) {
processDirectory(fullPath)
} else if (
entry.isFile() &&
entry.name.endsWith(".js") &&
entry.name !== "sf-to-darkroom.js"
) {
processFile(fullPath)
}
})
}
console.log("Detecting package manager...")
const packageManager = detectPackageManager()
console.log(`Package manager detected: ${packageManager}`)
console.log("Reading package.json...")
const packageJsonPath = path.join(process.cwd(), "package.json")
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"))
console.log("Updating dependencies...")
const dependencies = packageJson.dependencies || {}
const updatedDependencies = {}
for (const [packageName, version] of Object.entries(dependencies)) {
if (packageName.startsWith("@studio-freight/")) {
if (
packageName === "@studio-freight/lenis" ||
packageName === "@studio-freight/react-lenis"
) {
console.log(`Updating ${packageName} to lenis...`)
const latestVersion = getLatestPackageVersion("lenis")
updatedDependencies["lenis"] = `^${latestVersion}`
} else {
const newPackageName = packageName.replace(
"@studio-freight/",
"@darkroom.engineering/"
)
console.log(`Updating ${packageName} to ${newPackageName}...`)
const latestVersion = getLatestPackageVersion(newPackageName)
updatedDependencies[newPackageName] = `^${latestVersion}`
}
} else {
updatedDependencies[packageName] = version
}
}
console.log("Updating package.json...")
packageJson.dependencies = updatedDependencies
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), "utf8")
console.log("Removing @studio-freight packages from the lockfile...")
if (packageManager === "npm") {
execSync("npm install --package-lock-only")
} else if (packageManager === "yarn") {
execSync("yarn install --mode=update-lockfile")
} else if (packageManager === "pnpm") {
execSync("pnpm install --lockfile-only")
}
console.log("Installing updated dependencies...")
execSync(`${packageManager} install`)
console.log("Processing files...")
processDirectory(process.cwd())
console.log(
"Package import renaming, dependency updates, and lockfile cleanup completed!"
)