import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as klawSync from 'klaw-sync' ; import { config } from './config' ; import { pluralize } from './format' ; import { logger } from './logger' ; function getName ( thing ) { return typeof thing === 'string' ? thing : thing . name ; } export function pruneProjectIndex ( ) { const projectIndex = config . get ( 'projectIndex' ) ; for ( const name in projectIndex ) { const directory = path . resolve ( process . cwd ( ) , name ) ; if ( ! fs . existsSync ( directory ) ) { delete projectIndex [ name ] ; } } config . set ( 'projectIndex' , projectIndex ) ; } export function addToProjectIndex ( project ) { const projectIndex = config . get ( 'projectIndex' ) ; projectIndex [ project . name ] = project . projectId ; config . set ( 'projectIndex' , projectIndex ) ; } export function removeFromProjectIndex ( project ) { const projectIndex = config . get ( 'projectIndex' ) ; if ( getName ( project ) in projectIndex ) { delete projectIndex [ getName ( project ) ] ; config . set ( 'projectIndex' , projectIndex ) ; return true ; } return false ; } export function normalizePath ( filePath ) { return path . normalize ( filePath ) . replace ( /\\/g , '/' ) ; } export function getProjectPath ( project ) { return path . resolve ( process . cwd ( ) , getName ( project ) ) ; } export function getProjectFilePath ( project , file ) { return path . resolve ( getProjectPath ( project ) , getName ( file ) ) ; } export function getProjectName ( file ) { const relativePath = normalizePath ( path . relative ( process . cwd ( ) , getName ( file ) ) ) ; let bestMatch = '' ; for ( const name in config . get ( 'projectIndex' ) ) { if ( relativePath . startsWith ( name ) && ( bestMatch === '' || name . length > bestMatch . length ) ) { bestMatch = name ; } } return bestMatch ; } export function getFilePathRelativeToProject ( project , file ) { return normalizePath ( path . relative ( getProjectPath ( project ) , getName ( file ) ) ) ; } export function getFilesInProject ( project ) { const paths = klawSync ( getProjectPath ( project ) , { nodir : true } ) ; return paths . map ( path => getFilePathRelativeToProject ( project , path . path ) ) ; } export function warnAboutLibraryFiles ( libraryFiles , action ) { const fileCount = libraryFiles . length ; if ( fileCount === 0 ) { return ; } const filesStr = pluralize ( fileCount , 'file' ) ; const wereStr = pluralize ( fileCount , 'was' , 'were' ) ; const theseStr = pluralize ( fileCount , 'this' , 'these' ) ; const theirStr = pluralize ( fileCount , 'its' , 'their' ) ; const projectsStr = pluralize ( fileCount , 'project' ) ; logger . warn ( 'QuantConnect CLI no longer treats files from linked libraries as in-project files' ) ; logger . warn ( `For that reason the following ${ filesStr } ${ wereStr } not ${ action }:` ) ; for ( const file of libraryFiles ) { logger . warn ( `- ${ file }` ) ; } logger . warn ( `You can safely remove ${ theseStr } ${ filesStr } from your local disk` ) ; logger . warn ( `In the future please edit ${ theseStr } ${ filesStr } in ${ theirStr } ${ projectsStr } in the 'Library' directory` ) ; }