Skip to content

Commit

Permalink
feat: auto detect logged user OSTS url. issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jan 23, 2023
1 parent 88076c9 commit addd075
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 64 deletions.
118 changes: 62 additions & 56 deletions src/osts-cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import minimist, {ParsedArgs } from "minimist";
import minimist, { ParsedArgs } from "minimist";
import { exit } from "process";
import { pack } from './osts-pack.js'
import { unpack } from './osts-unpack.js'
import { askForPreferences, copyOfficeScriptSimplifiedDeclaration, listOfficeScript, savePreferences } from "./osts-utils.js";

const DEFAULT_PATH = 'osts'

type OSTSCLI = ParsedArgs
type OSTSCLI = ParsedArgs

function help() {
console.log(`
Expand All @@ -23,71 +23,77 @@ osts dts [--path, -p <dest dir>] // an Office Script Simplified TS Declaration f
`)
}

// Evaluate command
(async() => {
async function main() {

const cli:OSTSCLI = minimist( process.argv.slice(2), {
'--':false,
const cli: OSTSCLI = minimist(process.argv.slice(2), {
'--': false,
string: 'path',
boolean: 'dts',
alias: { 'p': 'path'},
'default': { 'path' : DEFAULT_PATH},
unknown: (args: string) => /pack|unpack|list|dts/i.test(args)
alias: { 'p': 'path' },
'default': { 'path': DEFAULT_PATH },
unknown: (args: string) => /pack|unpack|list|dts/i.test(args)
})

// console.log( cli );

// Check Arguments
const _cmd = () =>
(cli._.length > 0) ? cli._[0].toLowerCase() : undefined
const _path = () =>
const _cmd = () =>
(cli._.length > 0) ? cli._[0].toLowerCase() : undefined

const _path = () =>
cli['path']?.length === 0 ? DEFAULT_PATH : cli.path!

const command = _cmd()

// console.log( 'command', command, 'path', cli['path'], _path() )

if (command === 'list') {
const prefs = await askForPreferences()
if (!prefs) exit(-1)

const result = await listOfficeScript(prefs)

console.table(result.map(file => ({
Name: file.Name,
Length: `${file.Length} bytes`,
RelativeUrl: path.dirname(path.relative(prefs.toPath(), file.ServerRelativeUrl))
})))

savePreferences(prefs)

exit(0)
}
else if (command === 'pack') {
const code = await pack(_path())
exit(code)
}
else if (command === 'unpack') {
const code = await unpack(_path(), cli['dts'])
exit(code)
}
else if (command === 'dts') {
const code = await copyOfficeScriptSimplifiedDeclaration(_path())
exit(code)
}
else {
help()
exit(0)
}

}


// Evaluate command
(async () => {
try {
await main()

} catch (e) {
console.error('error occurred!', e)
exit(-1)
}


const command = _cmd()

// console.log( 'command', command, 'path', cli['path'], _path() )

if( command === 'list' ) {
const prefs = await askForPreferences()
if( !prefs ) exit(-1)

const result = await listOfficeScript( prefs )

console.table( result.map( file => ({
Name: file.Name,
Length: `${file.Length} bytes`,
RelativeUrl: path.dirname(path.relative( prefs.toPath(), file.ServerRelativeUrl ))
})) )

savePreferences( prefs )

exit(0)
}
else if( command === 'pack') {
const code = await pack( _path() )
exit(code)
}
else if( command === 'unpack' ) {
const code = await unpack( _path(), cli['dts'] )
exit(code)
}
else if( command === 'dts' ) {
const code = await copyOfficeScriptSimplifiedDeclaration( _path() )
exit(code)
}
else {
help()
exit(0)
}
}
catch( e ) {
console.error( 'error occurred!', e)
exit(-1)
}

})()


49 changes: 41 additions & 8 deletions src/osts-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,25 @@ const _prefs = new Preferences('org.bsc.officescripts-cli',{}, {

export const askForPreferences = async ():Promise<PreferenceData|undefined> => {

const candidateWebUrl = await askForWebUrl( _prefs )
if( !candidateWebUrl ) return
const candidateFolder = await askForFolder( _prefs )
if( !candidateFolder ) return
// const candidateWebUrl = await askForWebUrl( _prefs )
// if( !candidateWebUrl ) return
// const candidateFolder = await askForFolder( _prefs )
// if( !candidateFolder ) return

const candidateWebUrl = await getWebUrl()
const candidateFolder = path.join( 'Documents', 'Documents', 'Office Scripts')

return {
weburl:candidateWebUrl,
folder:candidateFolder,
toPath: () => path.join( new URL( candidateWebUrl ).pathname, candidateFolder)
}

}

export const savePreferences = ( data:PreferenceData) => {

_prefs.weburl = data.weburl
_prefs.folder = data.folder
// _prefs.weburl = data.weburl
// _prefs.folder = data.folder

}

Expand Down Expand Up @@ -182,4 +184,35 @@ export async function copyOfficeScriptSimplifiedDeclaration( bodyDirPath:string
const spoFileListResult = JSON.parse( result.stdout ) as Array<SPOFile>

return spoFileListResult
}
}

interface Connected {
connectedAs: string
}

type Status = Connected | "Logged out"

export const getWebUrl = async () => {

const cmd_status = await $`m365 status`.quiet()

const status = JSON.parse(cmd_status.stdout) as Status

if( typeof(status)==='string' ) {
throw "User not connected!"
}

const args = [
'--query', `[?Owner=='${status.connectedAs}'].Url | [0]`
]

const cmd_list = await $`m365 onedrive list ${args}`.quiet()

const url = JSON.parse(cmd_list.stdout)

if( url === null ) {
throw `Owner '${status.connectedAs}' not found!`
}

return url
}

0 comments on commit addd075

Please sign in to comment.