-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#210 Added scanning support for linux (testing needed)
- Loading branch information
Showing
4 changed files
with
179 additions
and
80 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
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,74 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export default new class { | ||
#searchDirs = ['/home', '/opt', '/var', '/usr/local']; | ||
|
||
/** | ||
* Gets all dever.json paths on linux | ||
* @returns {Promise<string[]>} paths | ||
*/ | ||
async scan() { | ||
// TODO: Needs testing | ||
const paths = []; | ||
|
||
for (const dir of this.#searchDirs) { | ||
const foundPaths = this.#findReposAndJsonFiles(dir); | ||
paths.push(...foundPaths); | ||
} | ||
|
||
return paths; | ||
} | ||
|
||
/** | ||
* Finds folder to scan | ||
* @param dir {string} | ||
* @returns {string[]} | ||
*/ | ||
#findReposAndJsonFiles(dir) { | ||
const items = fs.readdirSync(dir); | ||
|
||
// TODO: Does it have to be a git repository folder? | ||
if (items.includes('.git')) { | ||
const gitDir = path.join(dir, '.git'); | ||
return this.#searchForJsonFiles(gitDir); | ||
} | ||
|
||
const paths = []; | ||
|
||
for (const item of items) { | ||
const itemPath = path.join(dir, item); | ||
if (fs.statSync(itemPath).isDirectory()) { | ||
if (item === 'node_modules' || item === '.git' || item === '.svn' || item === '.hg' || item === 'bower_components') { | ||
continue; | ||
} | ||
|
||
const foundPaths = this.#findReposAndJsonFiles(itemPath); | ||
paths.push(...foundPaths); | ||
} | ||
} | ||
|
||
return paths; | ||
} | ||
|
||
/** | ||
* Searches for dever.json files | ||
* @param dir {string} | ||
* @returns {string[]} | ||
*/ | ||
#searchForJsonFiles(dir) { | ||
const jsonFiles = []; | ||
const items = fs.readdirSync(dir); | ||
|
||
for (const item of items) { | ||
const itemPath = path.join(dir, item); | ||
const stats = fs.statSync(itemPath); | ||
|
||
if (stats.isFile() && path.extname(item) === 'dever.json') { | ||
jsonFiles.push(itemPath); | ||
} | ||
} | ||
|
||
return jsonFiles; | ||
} | ||
} |
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,92 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import {execSync} from "child_process"; | ||
|
||
export default new class { | ||
/** | ||
* Gets all dever.json paths on windows | ||
* @returns {Promise<string[]>} paths | ||
*/ | ||
async scan() { | ||
const paths = []; | ||
const disks = this.#getDisks(); | ||
|
||
for (const disk of disks) { | ||
const files = this.#findFilesByName(disk, 'dever.json'); | ||
paths.push(...files); | ||
} | ||
|
||
return paths; | ||
} | ||
|
||
/** | ||
* Check if the folder should be skipped | ||
* @param folderName {string} | ||
* @returns {boolean} | ||
*/ | ||
#isSkipFolder(folderName) { | ||
return ['Windows', 'Program Files', 'Program Files (x86)', 'ProgramData', 'AppData', 'node_modules'].includes(folderName); | ||
} | ||
|
||
/** | ||
* Find all files with the given name | ||
* @param rootPath {string} | ||
* @param filename {string} | ||
* @param files {string[]} | ||
* @returns {string[]} | ||
*/ | ||
#findFilesByName(rootPath, filename, files = []) { | ||
try { | ||
if (fs.existsSync(rootPath)) { | ||
const contents = fs.readdirSync(rootPath); | ||
|
||
for (const file of contents) { | ||
const filePath = path.join(rootPath, file); | ||
|
||
try { | ||
const fileStat = fs.statSync(filePath); | ||
|
||
if (fileStat.isDirectory()) { | ||
if (!this.#isSkipFolder(file)) { | ||
this.#findFilesByName(filePath, filename, files); | ||
} | ||
} else if (file === filename) { | ||
files.push(filePath); | ||
} | ||
} catch (err) { | ||
if (err.code !== 'EPERM' && err.code !== 'EBUSY' && err.code !== 'EACCES' && err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
if (err.code !== 'EPERM' && err.code !== 'EACCES' && err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
|
||
/** | ||
* Get all local disks volume names | ||
* @returns {Array<string>} | ||
*/ | ||
#getDisks() { | ||
const disks = []; | ||
|
||
const output = execSync('wmic logicaldisk get deviceid, volumename, description, mediatype').toString(); | ||
const lines = output.trim().split('\r\n').slice(1); | ||
|
||
for (const line of lines) { | ||
const [_, volumeName, mediaType] = line.trim().split(/\s{2,}/); | ||
|
||
if (mediaType === '12') { | ||
disks.push(volumeName + '\\'); | ||
} | ||
} | ||
|
||
return disks; | ||
} | ||
} |