@the-vcsi/msgraph 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/index.js +18 -10
- package/src/templates/fetch-assets.js +116 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@the-vcsi/msgraph",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "sv CLI add-on for Microsoft Graph / SharePoint integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
},
|
|
9
9
|
"files": ["src"],
|
|
10
10
|
"keywords": ["sv-add", "svelte", "sveltekit", "microsoft-graph", "sharepoint"],
|
|
11
|
-
"dependencies": {
|
|
12
|
-
|
|
11
|
+
"dependencies": {},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"sv": "^0.13.0"
|
|
13
14
|
},
|
|
14
15
|
"repository": {
|
|
15
16
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineAddon, defineAddonOptions } from 'sv
|
|
1
|
+
import { defineAddon, defineAddonOptions } from 'sv';
|
|
2
2
|
import { readFileSync } from 'fs';
|
|
3
3
|
import { dirname, join } from 'path';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
@@ -8,6 +8,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
8
8
|
// Read templates from files (testable, lintable)
|
|
9
9
|
const FETCH_SCRIPT = readFileSync(join(__dirname, 'templates/fetch-msgraph.js'), 'utf8');
|
|
10
10
|
const APP_SETTINGS = readFileSync(join(__dirname, 'templates/appSettings.js'), 'utf8');
|
|
11
|
+
const ASSETS_SCRIPT = readFileSync(join(__dirname, 'templates/fetch-assets.js'), 'utf8');
|
|
11
12
|
const ENV_EXAMPLE = readFileSync(join(__dirname, 'templates/.env.example'), 'utf8');
|
|
12
13
|
|
|
13
14
|
const options = defineAddonOptions()
|
|
@@ -19,12 +20,16 @@ const options = defineAddonOptions()
|
|
|
19
20
|
|
|
20
21
|
export default defineAddon({
|
|
21
22
|
id: '@the-vcsi/msgraph',
|
|
23
|
+
shortDescription: 'Microsoft Graph / SharePoint integration for fetching story content',
|
|
22
24
|
options,
|
|
23
25
|
|
|
24
26
|
run: ({ sv, options: opts }) => {
|
|
25
27
|
// Create the fetch script
|
|
26
28
|
sv.file('scripts/fetch-msgraph.js', () => FETCH_SCRIPT);
|
|
27
29
|
|
|
30
|
+
// Pull binary assets (headshots, logos) out of the same SharePoint site
|
|
31
|
+
sv.file('scripts/fetch-assets.js', () => ASSETS_SCRIPT);
|
|
32
|
+
|
|
28
33
|
// Create app settings config with user-provided siteId
|
|
29
34
|
const appSettings = APP_SETTINGS.replace(/__SITE_ID__/g, opts.siteId || 'YOUR_SITE_ID');
|
|
30
35
|
sv.file('src/appSettings.js', () => appSettings);
|
|
@@ -42,23 +47,26 @@ export default defineAddon({
|
|
|
42
47
|
const pkg = JSON.parse(content);
|
|
43
48
|
pkg.scripts = pkg.scripts || {};
|
|
44
49
|
pkg.scripts['fetch:sharepoint'] = 'node scripts/fetch-msgraph.js';
|
|
50
|
+
pkg.scripts['fetch:headshots'] = 'node scripts/fetch-assets.js';
|
|
45
51
|
|
|
46
52
|
// Add required dependencies
|
|
47
53
|
pkg.dependencies = pkg.dependencies || {};
|
|
48
54
|
pkg.dependencies['@azure/identity'] = '^4.0.0';
|
|
49
55
|
pkg.dependencies['@microsoft/microsoft-graph-client'] = '^3.0.0';
|
|
50
56
|
pkg.dependencies['dotenv'] = '^16.0.0';
|
|
57
|
+
// fetch-assets.js converts SharePoint's .webp to the .jpg the templates request
|
|
58
|
+
pkg.dependencies['sharp'] = '^0.34.5';
|
|
51
59
|
|
|
52
60
|
return JSON.stringify(pkg, null, 2);
|
|
53
61
|
});
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
nextSteps: () => [
|
|
66
|
+
'Copy .env.example to .env',
|
|
67
|
+
'Get credentials from Azure Portal > App registrations',
|
|
68
|
+
'Run npm install',
|
|
69
|
+
'Run npm run fetch:sharepoint to pull story copy',
|
|
70
|
+
'Run npm run fetch:headshots to pull images (edit ASSET_FOLDERS first)'
|
|
71
|
+
]
|
|
64
72
|
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import 'dotenv/config';
|
|
4
|
+
import { mkdirSync } from 'fs';
|
|
5
|
+
import { writeFile } from 'fs/promises';
|
|
6
|
+
import { join, dirname, extname, basename } from 'path';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
import { ClientSecretCredential } from '@azure/identity';
|
|
9
|
+
import { Client } from '@microsoft/microsoft-graph-client';
|
|
10
|
+
import { TokenCredentialAuthenticationProvider } from '@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials/index.js';
|
|
11
|
+
import sharp from 'sharp';
|
|
12
|
+
import settings from '../src/appSettings.js';
|
|
13
|
+
|
|
14
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
const STATIC_DIR = join(__dirname, '..', 'static');
|
|
16
|
+
|
|
17
|
+
// Which SharePoint folders to pull, as { remotePath: localPathUnderStatic }.
|
|
18
|
+
// Edit this for your site: remotePath is relative to the document library root,
|
|
19
|
+
// localPath is relative to static/.
|
|
20
|
+
//
|
|
21
|
+
// Filenames become the local filenames, and .webp is converted to .jpg (the
|
|
22
|
+
// templates' member components request `<member id>.jpg`). So a headshot is
|
|
23
|
+
// only picked up if its filename matches the person's id in members.csv;
|
|
24
|
+
// rename it in SharePoint rather than guessing here.
|
|
25
|
+
const ASSET_FOLDERS = {
|
|
26
|
+
'assets/headshots': 'common/assets/members',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function createClient() {
|
|
30
|
+
const credential = new ClientSecretCredential(
|
|
31
|
+
settings.tenantId || process.env.tenantId,
|
|
32
|
+
settings.clientId || process.env.clientId,
|
|
33
|
+
settings.clientSecret || process.env.clientSecret
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
|
|
37
|
+
scopes: settings.scopes,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return Client.initWithMiddleware({ authProvider });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function downloadFile(client, driveId, itemId) {
|
|
44
|
+
// Get the download URL and fetch content
|
|
45
|
+
const response = await client
|
|
46
|
+
.api(`/drives/${driveId}/items/${itemId}/content`)
|
|
47
|
+
.responseType('arraybuffer')
|
|
48
|
+
.get();
|
|
49
|
+
|
|
50
|
+
return Buffer.from(response);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function main() {
|
|
54
|
+
const client = createClient();
|
|
55
|
+
|
|
56
|
+
// Get site info
|
|
57
|
+
let site;
|
|
58
|
+
try {
|
|
59
|
+
site = await client.api(`/sites/${settings.siteId}`).get();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error(`Error: ${error.code} - ${error.message}`);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(`Site: ${site.webUrl}`);
|
|
66
|
+
|
|
67
|
+
// Get drives
|
|
68
|
+
const drives = await client.api(`/sites/${site.id}/drives`).get();
|
|
69
|
+
const driveId = drives.value[0].id;
|
|
70
|
+
|
|
71
|
+
for (const [remotePath, localPath] of Object.entries(ASSET_FOLDERS)) {
|
|
72
|
+
console.log(`\nSyncing: ${remotePath} -> ${localPath}`);
|
|
73
|
+
|
|
74
|
+
const outputDir = join(STATIC_DIR, localPath);
|
|
75
|
+
mkdirSync(outputDir, { recursive: true });
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
// Get items in the folder
|
|
79
|
+
const items = await client
|
|
80
|
+
.api(`/drives/${driveId}/root:/${remotePath}:/children`)
|
|
81
|
+
.get();
|
|
82
|
+
|
|
83
|
+
const files = items.value.filter((item) => item.file);
|
|
84
|
+
console.log(`Found ${files.length} file(s)`);
|
|
85
|
+
|
|
86
|
+
for (const file of files) {
|
|
87
|
+
console.log(` Downloading: ${file.name}`);
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const content = await downloadFile(client, driveId, file.id);
|
|
91
|
+
const ext = extname(file.name).toLowerCase();
|
|
92
|
+
const name = basename(file.name, ext);
|
|
93
|
+
|
|
94
|
+
// Convert webp to jpg, keep other formats as-is
|
|
95
|
+
if (ext === '.webp') {
|
|
96
|
+
const jpgBuffer = await sharp(content).jpeg({ quality: 90 }).toBuffer();
|
|
97
|
+
const outputName = `${name}.jpg`;
|
|
98
|
+
await writeFile(join(outputDir, outputName), jpgBuffer);
|
|
99
|
+
console.log(` -> ${localPath}/${outputName} (converted from webp)`);
|
|
100
|
+
} else {
|
|
101
|
+
await writeFile(join(outputDir, file.name), content);
|
|
102
|
+
console.log(` -> ${localPath}/${file.name}`);
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error(` Error downloading: ${error.message}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error(`Error accessing folder: ${error.message}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
console.log('\nDone!');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
main().catch(console.error);
|