Skip to content

Commit

Permalink
startup scripts cleaned and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Char1zardd committed Mar 6, 2022
1 parent f73214d commit c6b4c05
Show file tree
Hide file tree
Showing 11 changed files with 22,684 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .tests/Launch-Scripts/createShortcut.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ describe('createShortcut', () => {
'Shortcuts not supported on your system'
)
})
})
})
14 changes: 12 additions & 2 deletions .tests/Launch-Scripts/runSetup.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const nock = require('nock')
const simpleGit = require('simple-git')
const process = require('process')
const env = require('../../Environment').newEnvironment()
const externalScriptsURLs = env.EXTERNAL_SCRIPTS
const {
Expand All @@ -10,7 +11,8 @@ const {
const {
setUpstreamAndOrigin,
runSetup,
installExternalScripts
installExternalScripts,
errorResp
} = require('../../Launch-Scripts/runSetup')

// ****** VERY IMPORTANT NOTE *******
Expand Down Expand Up @@ -49,7 +51,8 @@ simpleGit.mockReturnValue({

jest.mock('process', () => {
return {
cwd: jest.fn(() => './')
cwd: jest.fn(() => './'),
exit: jest.fn(() => 'there was an error')
}
})

Expand All @@ -76,6 +79,13 @@ afterEach(() => {
nock.restore()
})

describe('errorResp', () => {
it('should exit if error caught', () => {
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
errorResp('there was an error')
expect(mockExit).toHaveBeenCalledWith()
})
})
describe('setUpstreamAndOrigin', () => {
it('should return success msg if github is setup correctly', async () => {
const resp = await setUpstreamAndOrigin('./')
Expand Down
38 changes: 38 additions & 0 deletions .tests/Launch-Scripts/tfSetup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const tfSetup = require('../../Launch-Scripts/tfSetup')
const os = require('os')

jest.mock('os', () => {
return {
platform: jest.fn(() => 'win32')
}
})

jest.mock('fs', () => {
return {
copyFile: jest.fn(() => 'true'),
existsSync: jest.fn(() => true),
readdirSync: jest.fn(() => {
return {
filter: jest.fn((dirent) => {
return {
map: jest.fn((dirent) => 'dirname')
}
})
}
})
}
})

afterEach(() => {
jest.clearAllMocks()
})

describe('tfSetup', () => {
it('should copy tensorflow.dll for windows sytems', () => {
expect(tfSetup()).toEqual('tensorflow dll copied')
})
it('should do nothing if not windows system', () => {
jest.spyOn(os, 'platform').mockReturnValue('linux')
expect(tfSetup()).toEqual('non-windows system')
})
})
30 changes: 22 additions & 8 deletions Launch-Scripts/runSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const externalScriptsDir = path.join(process.cwd(), 'Platform', 'WebServer', 'ex
const env = require('../Environment').newEnvironment()
const externalScriptsURLs = env.EXTERNAL_SCRIPTS
const projectPluginMap = require('../Plugins/project-plugin-map.json')
const simpleGit = require('simple-git')

const errorResp = (e) => {
console.error(e)
process.exit(1)
process.exit()
}

// ** export setup piece by piece for more robust tests **
Expand All @@ -35,12 +34,13 @@ const installExternalScripts = () => {
}
})
}
console.log('External scripts installed')
return 'External scripts installed'
}

const setUpstreamAndOrigin = async (dir, repo='Superalgos') => {
console.log('Setting up github upstream and origin...')
// initialize simpleGit
const simpleGit = require('simple-git')
const options = {
binary: 'git',
maxConcurrentProcesses: 6,
Expand Down Expand Up @@ -124,8 +124,23 @@ const setUpstreamAndOrigin = async (dir, repo='Superalgos') => {
return 'Set upstream and origin for github'
}

const runSetup = () => {
const runSetup = (tfjs=false) => {
// Install Node_Modules to Main Superalgos Directory

// install tensorflow if user ran tensorflow setup file
if (tfjs !== false) {
console.log('Including tensorflow.js in your setup...')
nodeModulesDirs = [
path.join(process.cwd(),
"Projects",
"TensorFlow",
"TS",
"Bot-Modules",
"Learning-Bot",
"Low-Frequency-Learning")
]
}

let dir = process.cwd()
let command = 'echo Results of install at ' + dir + ' & npm ci'
let nodeInstPromise = new Promise(resolve => {
Expand Down Expand Up @@ -155,7 +170,7 @@ const runSetup = () => {

// Donload external scripts
console.log('')
console.log('Downloading external scripts …')
console.log('Setting up your environment …')
console.log('')
installExternalScripts()

Expand All @@ -165,19 +180,18 @@ const runSetup = () => {
// Ensure upstream and origin are set for this repo and submodules

let gitUser
let usesSSH = false
setUpstreamAndOrigin().then(async () => {
Object.values(projectPluginMap).forEach(plugin => {
setUpstreamAndOrigin(plugin.dir, plugin.repo)
})
}).catch(errorResp)
})
console.log('Setup complete')
return 'Setup complete'
}

module.exports = {
runSetup,
setUpstreamAndOrigin,
installExternalScripts
installExternalScripts,
errorResp
}
27 changes: 27 additions & 0 deletions Launch-Scripts/tfSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require('fs')
const os = require('os')
const path = require('path')

const tfSetup = () => {
const srcFolder = './node_modules/@tensorflow/tfjs-node/deps/lib/'
const destFolder = './node_modules/@tensorflow/tfjs-node/lib/'

if (os.platform() === 'win32' && fs.existsSync(srcFolder) && fs.existsSync(destFolder)) {
const dest = fs.readdirSync(destFolder, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)


fs.copyFile(path.join(srcFolder, "tensorflow.dll"), path.join(destFolder, dest[0], "tensorflow.dll"), (err) => {
if (err) {
console.log(err)
return err
}
console.log('tensorflow dll copied')
})
return 'tensorflow dll copied'
}
return 'non-windows system'
}

module.exports = tfSetup
Loading

0 comments on commit c6b4c05

Please sign in to comment.